Jaro-Winkler String Similarity
Score string similarity with matching windows, transpositions and a common-prefix boost for fuzzy name matching.
Code
export function jaroSimilarity(a: string, b: string): number {
if (a === b) return 1;
if (a.length === 0 || b.length === 0) return 0;
const window = Math.max(0, Math.floor(Math.max(a.length, b.length) / 2) - 1);
const aMatches = new Array<boolean>(a.length).fill(false);
const bMatches = new Array<boolean>(b.length).fill(false);
let matches = 0;
for (let i = 0; i < a.length; i += 1) {
const start = Math.max(0, i - window);
const end = Math.min(i + window + 1, b.length);
for (let j = start; j < end; j += 1) {
if (bMatches[j] || a[i] !== b[j]) continue;
aMatches[i] = true;
bMatches[j] = true;
matches += 1;
break;
}
}
if (matches === 0) return 0;
let transpositions = 0;
let cursor = 0;
for (let i = 0; i < a.length; i += 1) {
if (!aMatches[i]) continue;
while (!bMatches[cursor]) cursor += 1;
if (a[i] !== b[cursor]) transpositions += 1;
cursor += 1;
}
return (matches / a.length + matches / b.length + (matches - transpositions / 2) / matches) / 3;
}
export function jaroWinkler(a: string, b: string, prefixScale = 0.1): number {
if (prefixScale < 0 || prefixScale > 0.25) {
throw new RangeError("prefixScale must be between 0 and 0.25");
}
const jaro = jaroSimilarity(a, b);
let prefix = 0;
const limit = Math.min(4, a.length, b.length);
while (prefix < limit && a[prefix] === b[prefix]) prefix += 1;
return jaro + prefix * prefixScale * (1 - jaro);
}Inside the snippet
The working surface is the functions jaroSimilarity and jaroWinkler — everything else is wiring.
Iteration is nested: for (let i = 0; i < a.length; i += 1) frames the pass, for (let j = start; j < end; j += 1) does the per-item step. A guard precedes the RangeError: bad state stops the flow there instead of continuing.
What to watch for
- The snippet leans on
window— browser-only surfaces a bare Node process cannot provide. - The guard
a === bshort-circuits the flow with an earlyreturnwhen its condition holds.
Putting it to use
The primary signature is export function jaroSimilarity(a: string, b: string)
jaroSimilarity is what another module would consume; nothing else here is load-bearing for callers.
Core lines
return (matches / a.length + matches / b.length + (matches - transpositions / 2) / matches) / 3;
throw new RangeError("prefixScale must be between 0 and 0.25");
while (prefix < limit && a[prefix] === b[prefix]) prefix += 1;
Interactive Tools for This Topic
SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Vector Similarity CalculatorCompute Cosine Similarity, Dot Product, Euclidean L2, Manhattan L1, and Chebyshev distances between numeric vectors with dimension checks.
FAQ
- What happens if the preconditions do not hold?
- The code throws `RangeError` — the guard `a === b` rejects bad states before the main logic runs. Handle it at the call site or let it propagate deliberately.
- Does Jaro-Winkler String Similarity run in plain Node.js?
- No — it touches `window`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.