What is js-levenshtein?
The js-levenshtein npm package is used to calculate the Levenshtein distance between two strings. The Levenshtein distance is a measure of the difference between two sequences, which is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the other.
What are js-levenshtein's main functionalities?
Calculate Levenshtein distance
This feature allows you to calculate the Levenshtein distance between two strings. In the code sample, the distance between 'kitten' and 'sitting' is calculated, which is 3.
const levenshtein = require('js-levenshtein');
let distance = levenshtein('kitten', 'sitting');
console.log(distance); // Output: 3
Other packages similar to js-levenshtein
fast-levenshtein
This package provides a fast implementation of the Levenshtein algorithm. It is optimized for speed and is often faster than js-levenshtein, especially for longer strings.
levenshtein-edit-distance
This is another package that calculates the Levenshtein distance. It has a simple API and is also quite performant, but it may not be as optimized for speed as fast-levenshtein.
natural
Natural is a general natural language facility for Node.js. It includes a Levenshtein distance function as part of its broader set of language processing tools, which makes it more versatile than js-levenshtein if you need additional natural language processing features.
js-levenshtein
An efficient JS implementation calculating the Levenshtein distance, i.e. the difference between two strings.
Based on Wagner-Fischer dynamic programming algorithm, optimized for speed and memory
- use a single distance vector instead of a matrix
- loop unrolling on the outer loop
- remove common prefixes/postfixes from the calculation
- minimize the number of comparisons
Install
$ npm install --save js-levenshtein
Usage
const levenshtein = require('js-levenshtein');
levenshtein('kitten', 'sitting');
Benchmark
$ npm run bench
js-levenshtein
745,590 op/s » js-levenshtein/cached
667,378 op/s » js-levenshtein
533,400 op/s » leven
525,475 op/s » talisman
381,433 op/s » levenshtein-edit-distance
287,224 op/s » fast-levenshtein
80,848 op/s » levenshtein-component
24,762 op/s » levdist
27,333 op/s » ld
19,505 op/s » natural
13,413 op/s » levenshtein
License
MIT © Gustaf Andersson