CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-fastest-levenshtein

Fastest Levenshtein distance implementation in JS providing optimized algorithms for string edit distance calculations.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Fastest Levenshtein

Fastest 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.

Package Information

  • Package Name: fastest-levenshtein
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install fastest-levenshtein

Core Imports

import { 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";

Basic Usage

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)

Architecture

The library is built around two highly optimized algorithms:

  • Myers 32-bit Algorithm: Bit-parallel algorithm for strings ≤ 32 characters, providing maximum performance for short strings
  • Myers X Algorithm: Block-wise algorithm for longer strings using efficient memory access patterns
  • Automatic Selection: The library automatically chooses the optimal algorithm based on string length

Capabilities

String Distance Calculation

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 algorithm

Closest String Matching

Finds 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)
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/fastest-levenshtein@1.0.x
Publish Source
CLI
Badge
tessl/npm-fastest-levenshtein badge