ts-levenshtein - Levenshtein algorithm in Javascript

A Javascript implementation of the Levenshtein algorithm with locale-specific collator support. The core is an internal Myers bitâparallel implementation (no external runtime dependency), with smallâstring DP fast path and typedâarray buffer reuse.
Features
- Works in node.js and in the browser.
- Locale-sensitive string comparisons if needed.
- Comprehensive test suite.
Installation
npm install ts-levenshtein
CDN
You can also load from a CDN (minified IIFE build, global name TSLevenshtein):
- jsDelivr:
https://cdn.jsdelivr.net/npm/ts-levenshtein/dist/ts-levenshtein.global.js
- unpkg:
https://unpkg.com/ts-levenshtein/dist/ts-levenshtein.global.js
Examples
Default usage (Node.js)
const levenshtein = require("ts-levenshtein").default;
console.log(levenshtein.get("back", "book"));
console.log(levenshtein.get("ććä˝ ", "ćĺŤä˝ "));
Browser via CDN
<script src="https://cdn.jsdelivr.net/npm/ts-levenshtein/dist/ts-levenshtein.global.js"></script>
<script>
const d1 = TSLevenshtein.default.get('kitten', 'sitting');
const d2 = TSLevenshtein.default.get('ććä˝ ', 'ćĺŤä˝ ');
console.log(d1, d2);
</script>
// Note: CDN availability depends on publishing to npmjs.
// Optional: ESM via CDN loaders may vary by toolchain.
</script>
Locale-sensitive string comparisons
It supports using Intl.Collator for locale-sensitive string comparisons:
const levenshtein = require("ts-levenshtein").default;
levenshtein.get("mikailovitch", "MikhaĂŻlovitch", { useCollator: true });
Module formats and tooling
- CJS:
dist/ts-levenshtein.cjs
- ESM:
dist/ts-levenshtein.mjs
- CDN/IIFE (global):
dist/ts-levenshtein.global.js (global TSLevenshtein)
- TypeScript types:
dist/ts-levenshtein.d.ts (exposed via "types" field)
- Source maps: available for all builds
Building and Testing
To build the code and run the tests:
npm install
npm run build
npm test
Performance
Internals:
- Myers bitâparallel algorithm for the nonâcollator path (â¤32 uses 32âbit variant, longer strings use blocked variant)
- Smallâstring (â¤20) classic DP fast path
- Common prefix/suffix trimming
- Typed arrays (
Uint16Array/Int32Array) and fixedâcapacity buffer reuse to minimize allocations
Run the included benchmark:
npm run benchmark
Results vary by machine/Node version; expect competitive performance while keeping correctness and zero external deps.
Benchmark
Sample run (lower is faster):
Environment: Apple M4 MacBook, Node.js 22
| 1 | ts-levenshtein | 0.71 | 0.00% | ok |
| 2 | levenshtein-edit-distance | 1.82 | 156.34% | ok |
| 3 | levenshtein | 2.57 | 261.97% | ok |
| 4 | levenshtein-component | 3.18 | 347.89% | ok |
| 5 | levenshtein-deltas | 4.04 | 469.01% | ok |
| 6 | natural | 14.41 | 1929.58% | ok |
Contributing
If you wish to submit a pull request, please update and/or create new tests for any changes you make and ensure the build and tests pass locally (npm run build, npm test).
See CONTRIBUTING.md for details.
License
MIT - see LICENSE.md