fast-levenshtein
Advanced tools
Comparing version 2.0.6 to 3.0.0
@@ -10,2 +10,5 @@ (function() { | ||
} | ||
var levenshtein = require('fastest-levenshtein'); | ||
// arrays to re-use | ||
@@ -31,21 +34,21 @@ var prevRow = [], | ||
var str1Len = str1.length, | ||
str2Len = str2.length; | ||
// base cases | ||
if (str1Len === 0) return str2Len; | ||
if (str2Len === 0) return str1Len; | ||
if (useCollator) { | ||
var str1Len = str1.length, | ||
str2Len = str2.length; | ||
// base cases | ||
if (str1Len === 0) return str2Len; | ||
if (str2Len === 0) return str1Len; | ||
// two rows | ||
var curCol, nextCol, i, j, tmp; | ||
// two rows | ||
var curCol, nextCol, i, j, tmp; | ||
// initialise previous row | ||
for (i=0; i<str2Len; ++i) { | ||
prevRow[i] = i; | ||
str2Char[i] = str2.charCodeAt(i); | ||
} | ||
prevRow[str2Len] = str2Len; | ||
// initialise previous row | ||
for (i=0; i<str2Len; ++i) { | ||
prevRow[i] = i; | ||
str2Char[i] = str2.charCodeAt(i); | ||
} | ||
prevRow[str2Len] = str2Len; | ||
var strCmp; | ||
if (useCollator) { | ||
var strCmp; | ||
// calculate current row distance from previous row using collator | ||
@@ -81,36 +84,5 @@ for (i = 0; i < str1Len; ++i) { | ||
} | ||
return nextCol; | ||
} | ||
else { | ||
// calculate current row distance from previous row without collator | ||
for (i = 0; i < str1Len; ++i) { | ||
nextCol = i + 1; | ||
for (j = 0; j < str2Len; ++j) { | ||
curCol = nextCol; | ||
// substution | ||
strCmp = str1.charCodeAt(i) === str2Char[j]; | ||
nextCol = prevRow[j] + (strCmp ? 0 : 1); | ||
// insertion | ||
tmp = curCol + 1; | ||
if (nextCol > tmp) { | ||
nextCol = tmp; | ||
} | ||
// deletion | ||
tmp = prevRow[j + 1] + 1; | ||
if (nextCol > tmp) { | ||
nextCol = tmp; | ||
} | ||
// copy current col value into previous (in preparation for next iteration) | ||
prevRow[j] = curCol; | ||
} | ||
// copy last col value into previous (in preparation for next iteration) | ||
prevRow[j] = nextCol; | ||
} | ||
} | ||
return nextCol; | ||
return levenshtein.distance(str1, str2); | ||
} | ||
@@ -139,2 +111,1 @@ | ||
}()); | ||
{ | ||
"name": "fast-levenshtein", | ||
"version": "2.0.6", | ||
"version": "3.0.0", | ||
"description": "Efficient implementation of Levenshtein algorithm with locale-specific collator support.", | ||
@@ -38,3 +38,6 @@ "main": "levenshtein.js", | ||
"author": "Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"fastest-levenshtein": "^1.0.7" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # fast-levenshtein - Levenshtein algorithm in Javascript | ||
An efficient Javascript implementation of the [Levenshtein algorithm](http://en.wikipedia.org/wiki/Levenshtein_distance) with locale-specific collator support. | ||
A Javascript implementation of the [Levenshtein algorithm](http://en.wikipedia.org/wiki/Levenshtein_distance) with locale-specific collator support. This uses [fastest-levenshtein](https://github.com/ka-weihe/fastest-levenshtein) under the hood. | ||
@@ -14,27 +14,14 @@ ## Features | ||
* Works in node.js and in the browser. | ||
* 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)). | ||
* Locale-sensitive string comparisions if needed. | ||
* Comprehensive test suite and performance benchmark. | ||
* Small: <1 KB minified and gzipped | ||
* Locale-sensitive string comparisons if needed. | ||
* Comprehensive test suite. | ||
## Installation | ||
### node.js | ||
Install using [npm](http://npmjs.org/): | ||
```bash | ||
$ npm install fast-levenshtein | ||
``` | ||
**CDN** | ||
### Browser | ||
The latest version is now also always available at https://npm-cdn.com/pkg/fast-levenshtein/ | ||
Using bower: | ||
```bash | ||
$ bower install fast-levenshtein | ||
``` | ||
If you are not using any module loader system then the API will then be accessible via the `window.Levenshtein` object. | ||
## Examples | ||
@@ -74,26 +61,4 @@ | ||
_Thanks to [Titus Wormer](https://github.com/wooorm) for [encouraging me](https://github.com/hiddentao/fast-levenshtein/issues/1) to do this._ | ||
This uses [fastest-levenshtein](https://github.com/ka-weihe/fastest-levenshtein) under the hood. | ||
Benchmarked against other node.js levenshtein distance modules (on Macbook Air 2012, Core i7, 8GB RAM): | ||
```bash | ||
Running suite Implementation comparison [benchmark/speed.js]... | ||
>> levenshtein-edit-distance x 234 ops/sec ±3.02% (73 runs sampled) | ||
>> levenshtein-component x 422 ops/sec ±4.38% (83 runs sampled) | ||
>> levenshtein-deltas x 283 ops/sec ±3.83% (78 runs sampled) | ||
>> natural x 255 ops/sec ±0.76% (88 runs sampled) | ||
>> levenshtein x 180 ops/sec ±3.55% (86 runs sampled) | ||
>> fast-levenshtein x 1,792 ops/sec ±2.72% (95 runs sampled) | ||
Benchmark done. | ||
Fastest test is fast-levenshtein at 4.2x faster than levenshtein-component | ||
``` | ||
You can run this benchmark yourself by doing: | ||
```bash | ||
$ npm install | ||
$ npm run build | ||
$ npm run benchmark | ||
``` | ||
## Contributing | ||
@@ -100,0 +65,0 @@ |
7625
1
90
70
+ Addedfastest-levenshtein@^1.0.7
+ Addedfastest-levenshtein@1.0.16(transitive)