0
# Leven
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: leven
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript definitions)
9
- **Installation**: `npm install leven`
10
11
## Core Imports
12
13
```javascript
14
import leven from "leven";
15
```
16
17
## Basic Usage
18
19
```javascript
20
import leven from "leven";
21
22
// Basic string comparison
23
leven('cat', 'cow');
24
//=> 2
25
26
// Identical strings return 0
27
leven('hello', 'hello');
28
//=> 0
29
30
// One empty string
31
leven('', 'abc');
32
//=> 3
33
34
// Unicode support
35
leven('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文');
36
//=> 2
37
```
38
39
## Architecture
40
41
Leven uses an optimized implementation of the Levenshtein distance algorithm with several key performance features:
42
43
- **Prefix Trimming**: Removes common prefixes before calculation
44
- **Suffix Trimming**: Removes common suffixes before calculation
45
- **String Length Optimization**: Ensures the shorter string is processed first
46
- **Character Code Caching**: Caches character codes to minimize string operations
47
- **Memory Reuse**: Uses shared arrays to minimize memory allocation
48
49
## Capabilities
50
51
### Levenshtein Distance Calculation
52
53
Calculates the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another.
54
55
```javascript { .api }
56
/**
57
* Measure the difference between two strings using the Levenshtein distance algorithm
58
* @param first - The first string to compare
59
* @param second - The second string to compare
60
* @returns The Levenshtein distance between the two strings
61
*/
62
function leven(first: string, second: string): number;
63
```
64
65
**Parameters:**
66
- `first` (string): The first string to compare
67
- `second` (string): The second string to compare
68
69
**Returns:**
70
- `number`: The Levenshtein distance (minimum number of single-character edits required)
71
72
**Performance Characteristics:**
73
- Optimized for common cases with prefix/suffix trimming
74
- Efficient memory usage through array reuse
75
- Character code caching reduces string operations
76
- Time complexity optimizations for identical strings and empty inputs
77
78
**Usage Examples:**
79
80
```javascript
81
import leven from "leven";
82
83
// Simple character substitution
84
leven('a', 'b');
85
//=> 1
86
87
// Character insertions and deletions
88
leven('kitten', 'sitting');
89
//=> 3
90
91
// Complex transformations
92
leven('levenshtein', 'frankenstein');
93
//=> 6
94
95
// Edge cases
96
leven('', ''); // Empty strings
97
//=> 0
98
99
leven('abc', ''); // One empty string
100
//=> 3
101
102
// Unicode and international characters
103
leven('café', 'cafe');
104
//=> 1
105
```
106
107
**Common Use Cases:**
108
- Spell checking and correction
109
- Fuzzy string matching
110
- Search suggestion systems
111
- Data deduplication
112
- Text similarity analysis
113
- DNA sequence comparison
114
- Version comparison algorithms