Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including node-levenshtein with all npm packages installed. Try it out:

var nodeLevenshtein = require("node-levenshtein")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

node-levenshtein v2.0.3

Fastest Levenshtein distance implementation.

node-levenshtein :rocket:

Fastest implementation of the Levenshtein distance algorithm - (up to 8x+ faster than closest competitor). This algorithm can be used to measure the similarity of two strings. The performance has been achieved by using a highly-optimized N-API module written in C++. Furthermore, this module has the fastest javascript implementation that runs for short strings (when N-API overhead is too big).

Note: if you need to take the edit distance of one string against many strings (maybe billions), I have created a GPU-implementation that will be atleast 1000x faster than this. Contact me, if you're interested.

This package requires node v8.6.0 or higher.

Installation

$ npm install node-levenshtein --save

Usage

const levenshtein = require('node-levenshtein')

console.log(levenshtein('lorem', 'ipsum'))
//=> 4

Benchmark

I generated 500 pairs of strings with length N. I measured the ops/sec each library achieves to process all the given pairs. Higher is better. node-levenshtein is a lot faster in all cases.

Test TargetN=4N=8N=16N=32N=64N=128N=256N=512N=1024
node-levenshtein4242317237557734282013596.1182.649.8812.99
js-levenshtein24012126844100131137197.2525.406.4041.632
leven3029111002337890722759.9415.593.9580.998
talisman298599782331992623463.7616.123.9890.986
fast-levenshtein235177686229157615440.1310.162.5580.643
levenshtein-edit-distance3142310783320985121456.1314.243.6940.931

Relative Performance

This image shows the relative performance between node-levenshtein and js-levenshtein (the 2nd fastest). As you can see, node-levenshtein is a lot faster for small strings, but especially for long strings (8x faster when N = 1024).

Benchmark

License

This project is licensed under the MIT License - see the LICENSE.md file for details

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free