🚀 DAY 2 OF LAUNCH WEEK: Announcing Socket Certified Patches: One-Click Fixes for Vulnerable Dependencies.Learn more
Socket
Book a DemoInstallSign in
Socket

ts-levenshtein

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-levenshtein

Efficient implementation of Levenshtein algorithm with locale-specific collator support.

latest
Source
npmnpm
Version
1.0.7
Version published
Maintainers
1
Created
Source

ts-levenshtein - Levenshtein algorithm in TypeScript

CI npm version npm downloads Follow on Twitter

English | 简体中文 | العربية

A TypeScript 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 load either the global IIFE build or ESM via esm.sh.

  • IIFE (global TSLevenshtein):
    • jsDelivr: https://cdn.jsdelivr.net/npm/ts-levenshtein/dist/index.global.js
    • unpkg: https://unpkg.com/ts-levenshtein/dist/index.global.js
  • ESM (esm.sh):
    • https://esm.sh/ts-levenshtein

Examples

Default usage (Node.js)

// CommonJS (npm)
const levenshtein = require("ts-levenshtein").default;
console.log(levenshtein.get("back", "book")); // 2
console.log(levenshtein.get("我愛你", "我叫你")); // 1

// or ESM
// import levenshtein from 'ts-levenshtein'
// console.log(levenshtein.get('back', 'book'))

Browser via CDN (IIFE)

<script src="https://cdn.jsdelivr.net/npm/ts-levenshtein/dist/index.global.js"></script>
<script>
  // Global name: TSLevenshtein
  const d1 = TSLevenshtein.default.get('kitten', 'sitting');
  const d2 = TSLevenshtein.default.get('我愛你', '我叫你');
  console.log(d1, d2);
  // If you prefer unpkg:
  // <script src="https://unpkg.com/ts-levenshtein/dist/index.global.js"></script>
  // Note: CDN availability depends on publishing to npmjs.

  // Optional: ESM via CDN loaders may vary by toolchain.
</script>

Browser via CDN (ESM with esm.sh)

<script type="module">
  import levenshtein from 'https://esm.sh/ts-levenshtein?bundle';
  console.log(levenshtein.get('kitten', 'sitting'));
  console.log(levenshtein.get('我愛你', '我叫你'));
  // Tip: `?bundle` helps ensure a single file for browsers
  // Deno: import levenshtein from 'https://esm.sh/ts-levenshtein'
</script>

Locale-sensitive string comparisons

It supports using Intl.Collator for locale-sensitive string comparisons:

// CommonJS (npm)
const levenshtein = require("ts-levenshtein").default;
levenshtein.get("mikailovitch", "Mikhaïlovitch", { useCollator: true });
// 1

// or ESM
// import levenshtein from 'ts-levenshtein'
// levenshtein.get('mikailovitch', 'Mikhaïlovitch', { useCollator: true })

Module formats and tooling

  • CJS: dist/index.cjs
  • ESM: dist/index.mjs
  • UMD: dist/index.umd.js
  • IIFE (global): dist/index.global.js (global TSLevenshtein)
  • TypeScript types: dist/index.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

RankImplementationTime (ms)Relative to fastestStatus
1ts-levenshtein0.710.00%ok
2levenshtein-edit-distance1.82156.34%ok
3levenshtein2.57261.97%ok
4levenshtein-component3.18347.89%ok
5levenshtein-deltas4.04469.01%ok
6natural14.411929.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

Keywords

levenshtein

FAQs

Package last updated on 15 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts