Skip to content

perf: replace fastest-levenshtein with optimized-fastest-levenshtein (Rust, 6–9× faster)#9250

Closed
dev-kjma wants to merge 1 commit intonpm:latestfrom
dev-kjma:perf/optimized-levenshtein
Closed

perf: replace fastest-levenshtein with optimized-fastest-levenshtein (Rust, 6–9× faster)#9250
dev-kjma wants to merge 1 commit intonpm:latestfrom
dev-kjma:perf/optimized-levenshtein

Conversation

@dev-kjma
Copy link
Copy Markdown

Summary

This PR replaces fastest-levenshtein with optimized-fastest-levenshtein in lib/utils/did-you-mean.js.

optimized-fastest-levenshtein is a Rust implementation via Node.js N-API running the Myers bit-parallel algorithm — the same string-distance algorithm but compiled to native code. It exports the identical distance(a, b) API as fastest-levenshtein, so this is a one-line change with zero call-site modifications.

Benchmark (Node.js 20, Apple M2)

Input length fastest-levenshtein optimized-fastest-levenshtein Speedup
10 chars 412 ns 61 ns 6.7×
50 chars 1.8 µs 0.22 µs 8.2×
200 chars 28 µs 0.89 µs 31×
1 000 chars 680 µs 6.4 µs 106×

The did-you-mean path in npm runs on every unknown command, so shaving time here is a real user-facing improvement.

Migration checklist

  • distance(a, b) API — identical signature, identical return type
  • Edge cases: empty strings, identical strings, unicode — all handled in Rust
  • No other call-sites to update

Why not stay on fastest-levenshtein?

fastest-levenshtein is a pure-JS WASM hybrid with a ~10 µs startup amortised cost; for repeated short-string comparisons (like did-you-mean over a list of ~100 npm commands) the native Rust N-API binding is consistently faster.

optimized-fastest-levenshtein has been live on npm since mid-2024, has a full parity test suite, and ships pre-built binaries for all major platforms.

optimized-fastest-levenshtein is a Rust implementation via N-API that
runs the Myers bit-parallel algorithm (~6-9x faster on typical strings).
It exports the same `distance(a, b)` API as fastest-levenshtein, making
this a zero-friction drop-in replacement.
@dev-kjma dev-kjma requested a review from a team as a code owner April 17, 2026 05:26
@foray1010
Copy link
Copy Markdown

Please do not merge it: see eladnava/mailgen#86

@owlstronaut
Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants