| 1 | # fast-levenshtein - Levenshtein algorithm in Javascript
|
|---|
| 2 |
|
|---|
| 3 | [](http://travis-ci.org/hiddentao/fast-levenshtein)
|
|---|
| 4 | [](https://badge.fury.io/js/fast-levenshtein)
|
|---|
| 5 | [](https://www.npmjs.com/package/fast-levenshtein)
|
|---|
| 6 | [](https://twitter.com/hiddentao)
|
|---|
| 7 |
|
|---|
| 8 | An efficient Javascript implementation of the [Levenshtein algorithm](http://en.wikipedia.org/wiki/Levenshtein_distance) with locale-specific collator support.
|
|---|
| 9 |
|
|---|
| 10 | ## Features
|
|---|
| 11 |
|
|---|
| 12 | * Works in node.js and in the browser.
|
|---|
| 13 | * Better performance than other implementations by not needing to store the whole matrix ([more info](http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm)).
|
|---|
| 14 | * Locale-sensitive string comparisions if needed.
|
|---|
| 15 | * Comprehensive test suite and performance benchmark.
|
|---|
| 16 | * Small: <1 KB minified and gzipped
|
|---|
| 17 |
|
|---|
| 18 | ## Installation
|
|---|
| 19 |
|
|---|
| 20 | ### node.js
|
|---|
| 21 |
|
|---|
| 22 | Install using [npm](http://npmjs.org/):
|
|---|
| 23 |
|
|---|
| 24 | ```bash
|
|---|
| 25 | $ npm install fast-levenshtein
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | ### Browser
|
|---|
| 29 |
|
|---|
| 30 | Using bower:
|
|---|
| 31 |
|
|---|
| 32 | ```bash
|
|---|
| 33 | $ bower install fast-levenshtein
|
|---|
| 34 | ```
|
|---|
| 35 |
|
|---|
| 36 | If you are not using any module loader system then the API will then be accessible via the `window.Levenshtein` object.
|
|---|
| 37 |
|
|---|
| 38 | ## Examples
|
|---|
| 39 |
|
|---|
| 40 | **Default usage**
|
|---|
| 41 |
|
|---|
| 42 | ```javascript
|
|---|
| 43 | var levenshtein = require('fast-levenshtein');
|
|---|
| 44 |
|
|---|
| 45 | var distance = levenshtein.get('back', 'book'); // 2
|
|---|
| 46 | var distance = levenshtein.get('我愛你', '我叫你'); // 1
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | **Locale-sensitive string comparisons**
|
|---|
| 50 |
|
|---|
| 51 | It supports using [Intl.Collator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator) for locale-sensitive string comparisons:
|
|---|
| 52 |
|
|---|
| 53 | ```javascript
|
|---|
| 54 | var levenshtein = require('fast-levenshtein');
|
|---|
| 55 |
|
|---|
| 56 | levenshtein.get('mikailovitch', 'Mikhaïlovitch', { useCollator: true});
|
|---|
| 57 | // 1
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | ## Building and Testing
|
|---|
| 61 |
|
|---|
| 62 | To build the code and run the tests:
|
|---|
| 63 |
|
|---|
| 64 | ```bash
|
|---|
| 65 | $ npm install -g grunt-cli
|
|---|
| 66 | $ npm install
|
|---|
| 67 | $ npm run build
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | ## Performance
|
|---|
| 71 |
|
|---|
| 72 | _Thanks to [Titus Wormer](https://github.com/wooorm) for [encouraging me](https://github.com/hiddentao/fast-levenshtein/issues/1) to do this._
|
|---|
| 73 |
|
|---|
| 74 | Benchmarked against other node.js levenshtein distance modules (on Macbook Air 2012, Core i7, 8GB RAM):
|
|---|
| 75 |
|
|---|
| 76 | ```bash
|
|---|
| 77 | Running suite Implementation comparison [benchmark/speed.js]...
|
|---|
| 78 | >> levenshtein-edit-distance x 234 ops/sec ±3.02% (73 runs sampled)
|
|---|
| 79 | >> levenshtein-component x 422 ops/sec ±4.38% (83 runs sampled)
|
|---|
| 80 | >> levenshtein-deltas x 283 ops/sec ±3.83% (78 runs sampled)
|
|---|
| 81 | >> natural x 255 ops/sec ±0.76% (88 runs sampled)
|
|---|
| 82 | >> levenshtein x 180 ops/sec ±3.55% (86 runs sampled)
|
|---|
| 83 | >> fast-levenshtein x 1,792 ops/sec ±2.72% (95 runs sampled)
|
|---|
| 84 | Benchmark done.
|
|---|
| 85 | Fastest test is fast-levenshtein at 4.2x faster than levenshtein-component
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | You can run this benchmark yourself by doing:
|
|---|
| 89 |
|
|---|
| 90 | ```bash
|
|---|
| 91 | $ npm install
|
|---|
| 92 | $ npm run build
|
|---|
| 93 | $ npm run benchmark
|
|---|
| 94 | ```
|
|---|
| 95 |
|
|---|
| 96 | ## Contributing
|
|---|
| 97 |
|
|---|
| 98 | If you wish to submit a pull request please update and/or create new tests for any changes you make and ensure the grunt build passes.
|
|---|
| 99 |
|
|---|
| 100 | See [CONTRIBUTING.md](https://github.com/hiddentao/fast-levenshtein/blob/master/CONTRIBUTING.md) for details.
|
|---|
| 101 |
|
|---|
| 102 | ## License
|
|---|
| 103 |
|
|---|
| 104 | MIT - see [LICENSE.md](https://github.com/hiddentao/fast-levenshtein/blob/master/LICENSE.md)
|
|---|