1 # fast-levenshtein - Levenshtein algorithm in Javascript
3 [![Build Status](https://secure.travis-ci.org/hiddentao/fast-levenshtein.png)](http://travis-ci.org/hiddentao/fast-levenshtein)
5 An efficient Javascript implementation of the [Levenshtein algorithm](http://en.wikipedia.org/wiki/Levenshtein_distance) with asynchronous callback support.
9 * Works in node.js and in the browser.
10 * 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)).
11 * Provides synchronous and asynchronous versions of the algorithm.
12 * Asynchronous version is almost as fast as the synchronous version for small strings and can also provide progress updates.
13 * Comprehensive test suite and performance benchmark.
14 * Small: <1 KB minified and gzipped
20 Install using [npm](http://npmjs.org/):
23 $ npm install fast-levenshtein
31 $ bower install fast-levenshtein
34 If you are not using any module loader system then the API will then be accessible via the `window.Levenshtein` object.
41 var levenshtein = require('fast-levenshtein');
43 var distance = levenshtein.get('back', 'book'); // 2
44 var distance = levenshtein.get('我愛你', '我叫你'); // 1
50 var levenshtein = require('fast-levenshtein');
52 levenshtein.getAsync('back', 'book', function (err, distance) {
53 // err is null unless an error was thrown
58 **Asynchronous with progress updates**
61 var levenshtein = require('fast-levenshtein');
63 var hugeText1 = fs.readFileSync(...);
64 var hugeText2 = fs.readFileSync(...);
66 levenshtein.getAsync(hugeText1, hugeText2, function (err, distance) {
67 // process the results as normal
69 progress: function(percentComplete) {
70 console.log(percentComplete + ' % completed so far...');
75 ## Building and Testing
77 To build the code and run the tests:
80 $ npm install -g grunt-cli
87 _Thanks to [Titus Wormer](https://github.com/wooorm) for [encouraging me](https://github.com/hiddentao/fast-levenshtein/issues/1) to do this._
89 Benchmarked against other node.js levenshtein distance modules (on Macbook Air 2012, Core i7, 8GB RAM):
92 Running suite Implementation comparison [benchmark/speed.js]...
93 >> levenshtein-edit-distance x 234 ops/sec ±3.02% (73 runs sampled)
94 >> levenshtein-component x 422 ops/sec ±4.38% (83 runs sampled)
95 >> levenshtein-deltas x 283 ops/sec ±3.83% (78 runs sampled)
96 >> natural x 255 ops/sec ±0.76% (88 runs sampled)
97 >> levenshtein x 180 ops/sec ±3.55% (86 runs sampled)
98 >> fast-levenshtein x 1,792 ops/sec ±2.72% (95 runs sampled)
100 Fastest test is fast-levenshtein at 4.2x faster than levenshtein-component
103 You can run this benchmark yourself by doing:
106 $ npm install -g grunt-cli
114 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.
116 See [CONTRIBUTING.md](https://github.com/hiddentao/fast-levenshtein/blob/master/CONTRIBUTING.md) for details.
120 MIT - see [LICENSE.md](https://github.com/hiddentao/fast-levenshtein/blob/master/LICENSE.md)