fast-levenshtein
A tiny, locale-aware Levenshtein distance calculator for JavaScript, backed by the fastest-levenshtein engine.
Repository Health
Technical Analysis
fast-levenshtein computes the Levenshtein edit distance between two strings — the minimum number of single-character insertions, deletions, or substitutions needed to turn one string into the other. Under the hood it delegates to the fastest-levenshtein package for its default comparisons, giving it a highly optimized core while keeping a simple, stable public API that has shipped unchanged for years.
Beyond the default fast path, it adds one thing fastest-levenshtein doesn’t: locale-sensitive comparison via the native Intl.Collator API. Passing { useCollator: true } switches to a manual dynamic-programming implementation that treats characters as equivalent according to a given locale’s collation rules (for example, treating accented and unaccented forms of a letter as the same character), which is useful for fuzzy-matching user input, spell-checking, or deduplicating near-identical strings across locales.
What You Get
- Single function API - one
levenshtein.get(str1, str2, options)call returns the edit distance as an integer, no configuration required for the common case. - Optimized default path - delegates to fastest-levenshtein for its core distance calculation, so the common case runs on an actively maintained, high-performance implementation.
- Locale-aware comparison mode -
{ useCollator: true }switches to an Intl.Collator-backed comparison so accented and unaccented characters (or other locale-specific equivalences) count as identical. - Universal module support - ships with AMD, CommonJS, Web Worker, and browser-global wrappers so it drops into node.js, bundlers, or plain
<script>tags without extra config.
Common Use Cases
- Fuzzy search and autocomplete - rank or filter search suggestions by how close a query string is to candidate strings.
- Spell-checking and typo correction - measure how far a misspelled word is from dictionary entries to suggest corrections.
- Deduplication of near-identical records - compare product names, addresses, or user-submitted strings to detect near-duplicates during data cleaning.
- Locale-sensitive string matching - compare user input across accented/unaccented or locale-specific character variants using the collator mode.
Under The Hood
Architecture
fast-levenshtein is architected as a single-file UMD module (levenshtein.js) that exposes one object with a get(str1, str2, options) method. Its control flow is a simple branch: when options.useCollator is falsy or Intl.Collator failed to initialize, it delegates directly to the fastest-levenshtein package’s distance() function; when collator mode is requested, it runs its own hand-written two-row dynamic-programming implementation that reuses pre-allocated prevRow/str2Char arrays across calls to avoid repeated allocation. Module registration checks for AMD (define), CommonJS (module.exports), Web Worker (self.postMessage), and browser (window) environments in sequence, so the same file works unmodified across runtimes. There’s no external state beyond the shared scratch arrays, and no abstraction to fracture if this changed — the whole surface area is the one function.
Tech Stack
The runtime dependency surface is minimal: a single production dependency on fastest-levenshtein (^1.0.7) supplies the core distance algorithm, while the package itself only adds the collator-aware branch and the module-wrapper boilerplate. Development tooling is dated relative to the current JS ecosystem — Grunt (grunt, grunt-contrib-jshint, grunt-contrib-uglify, grunt-mocha-test) drives the build/test/minify pipeline, mocha (~1.9) and chai (~1.5) run the assertions, and .travis.yml configures a (now-defunct) Travis CI job. There’s no TypeScript, no bundler config, and no linter beyond JSHint via Grunt; the published artifact is the hand-maintained levenshtein.min.js alongside the source file.
Code Quality
Test coverage lives in test/tests.js, run via Mocha/Chai against the minified build (levenshtein.min), and is genuinely thorough for the library’s scope: it programmatically generates insert/delete/substitution cases across a sample string, adds explicit substitution and non-Latin (Chinese-character) cases, checks both collator-on and collator-off outcomes for the same accented-string pair, and includes a large-text performance/regression case comparing two multi-kilobyte text files. There’s no TypeScript or static type checking, no linter run as part of npm test (JSHint exists only in the Grunt build task), and the repository has had no commits since 2021 — CI (Travis) is defunct with no GitHub Actions workflow, so nothing currently verifies these tests automatically on new commits.
API Design
The public API is deliberately minimal: a single levenshtein.get(str1, str2, options) call with no required options object, mirroring the ergonomics of the fastest-levenshtein package it wraps. The one extension point, { useCollator: true }, is discoverable directly from the README’s second example and requires no additional setup — Intl.Collator is initialized once at module load and silently disabled if unavailable, so callers never need to feature-detect it themselves. The tradeoff is that the library exposes no distance-with-alignment, no configurable cost weights for insert/delete/substitute, and no async/streaming variant — it’s intentionally a one-function utility, which keeps the learning curve close to zero but limits it to exact-distance use cases.
Used by 4 apps in this directory
AnythingLLM
Developer Tools · Automation · AI Assistants
The all-in-one AI platform for private document chat, no-code agents, and local LLMs with zero setup friction.
Fern
Developer Tools
Fern turns a single OpenAPI, AsyncAPI, or Protobuf definition into type-safe SDKs for nine languages and a hosted API documentation site, all from one CLI and one source of truth.
NocoDB
No Code Platforms · Databases · Low Code Platforms
Turn any SQL database into a collaborative no-code spreadsheet with automatic REST APIs and real-time views.
Tabby
AI Code Assistants
Self-hosted AI coding assistant — run GitHub Copilot-grade code completion on your own hardware with no cloud dependency.