Fastest Levenshtein distance implementation in JS providing optimized algorithms for string edit distance calculations.
npx @tessl/cli install tessl/npm-fastest-levenshtein@1.0.0Fastest Levenshtein provides the most performant JavaScript/TypeScript implementation of the Levenshtein distance algorithm. It calculates the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another, essential for fuzzy string matching, spell checking, and data deduplication.
npm install fastest-levenshteinimport { distance, closest } from "fastest-levenshtein";For CommonJS:
const { distance, closest } = require("fastest-levenshtein");For Deno:
import { distance, closest } from "https://deno.land/x/fastest_levenshtein/mod.ts";import { distance, closest } from "fastest-levenshtein";
// Calculate edit distance between two strings
const editDistance = distance("fast", "faster");
console.log(editDistance); // 2
// Find closest match from array of candidates
const bestMatch = closest("fast", ["slow", "faster", "fastest"]);
console.log(bestMatch); // "faster"
// Handle edge cases
console.log(distance("", "hello")); // 5 (length of non-empty string)
console.log(distance("same", "same")); // 0 (identical strings)The library is built around two highly optimized algorithms:
Calculates the Levenshtein distance between two strings using optimized algorithms.
/**
* Calculates the Levenshtein distance between two strings
* @param a - First input string
* @param b - Second input string
* @returns The edit distance (minimum number of single-character edits needed)
*/
function distance(a: string, b: string): number;Usage Examples:
import { distance } from "fastest-levenshtein";
// Basic usage
console.log(distance("kitten", "sitting")); // 3
console.log(distance("book", "back")); // 2
// Edge cases
console.log(distance("", "")); // 0 (both empty)
console.log(distance("hello", "")); // 5 (one empty)
console.log(distance("test", "test")); // 0 (identical)
// Performance is optimal for all string lengths
console.log(distance("short", "strings")); // Uses myers_32 algorithm
console.log(distance("very long string example", "another very long string")); // Uses myers_x algorithmFinds the string in an array with the lowest edit distance to a target string.
/**
* Finds the string in an array with the lowest edit distance to a target string
* @param str - Target string to match against
* @param arr - Array of candidate strings (readonly)
* @returns The string from the array with the lowest edit distance
*/
function closest(str: string, arr: readonly string[]): string;Usage Examples:
import { closest } from "fastest-levenshtein";
// Find best match for typos/fuzzy search
const candidates = ["apple", "application", "apply", "appreciate"];
console.log(closest("aple", candidates)); // "apple"
// Spell checking
const dictionary = ["hello", "world", "help", "held"];
console.log(closest("helo", dictionary)); // "hello"
// Case sensitive matching
console.log(closest("Hello", ["hello", "HELLO", "Hello"])); // "Hello" (exact match)
// Handles ties by returning first occurrence
const colors = ["red", "blue", "green"];
console.log(closest("r", colors)); // "red" (first with distance 2)