or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-leven

Measure the difference between two strings using the Levenshtein distance algorithm

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/leven@4.0.x

To install, run

npx @tessl/cli install tessl/npm-leven@4.0.0

index.mddocs/

Leven

Leven is a highly optimized JavaScript library that calculates the Levenshtein distance between two strings. It implements several performance optimizations including prefix and suffix trimming to reduce computational workload by ignoring common prefixes and suffixes that don't contribute to the distance calculation.

Package Information

  • Package Name: leven
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install leven

Core Imports

import leven from "leven";

Basic Usage

import leven from "leven";

// Basic string comparison
leven('cat', 'cow');
//=> 2

// Identical strings return 0
leven('hello', 'hello');
//=> 0

// One empty string
leven('', 'abc');
//=> 3

// Unicode support
leven('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文');
//=> 2

Architecture

Leven uses an optimized implementation of the Levenshtein distance algorithm with several key performance features:

  • Prefix Trimming: Removes common prefixes before calculation
  • Suffix Trimming: Removes common suffixes before calculation
  • String Length Optimization: Ensures the shorter string is processed first
  • Character Code Caching: Caches character codes to minimize string operations
  • Memory Reuse: Uses shared arrays to minimize memory allocation

Capabilities

Levenshtein Distance Calculation

Calculates the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another.

/**
 * Measure the difference between two strings using the Levenshtein distance algorithm
 * @param first - The first string to compare
 * @param second - The second string to compare
 * @returns The Levenshtein distance between the two strings
 */
function leven(first: string, second: string): number;

Parameters:

  • first (string): The first string to compare
  • second (string): The second string to compare

Returns:

  • number: The Levenshtein distance (minimum number of single-character edits required)

Performance Characteristics:

  • Optimized for common cases with prefix/suffix trimming
  • Efficient memory usage through array reuse
  • Character code caching reduces string operations
  • Time complexity optimizations for identical strings and empty inputs

Usage Examples:

import leven from "leven";

// Simple character substitution
leven('a', 'b');
//=> 1

// Character insertions and deletions
leven('kitten', 'sitting');
//=> 3

// Complex transformations
leven('levenshtein', 'frankenstein');
//=> 6

// Edge cases
leven('', '');        // Empty strings
//=> 0

leven('abc', '');     // One empty string
//=> 3

// Unicode and international characters
leven('café', 'cafe');
//=> 1

Common Use Cases:

  • Spell checking and correction
  • Fuzzy string matching
  • Search suggestion systems
  • Data deduplication
  • Text similarity analysis
  • DNA sequence comparison
  • Version comparison algorithms