javascript
text similarity
algorithm
programming
natural language processing

Javascript text similarity algorithm

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

There is no single best text similarity algorithm in JavaScript because different algorithms answer different questions. If you want typo tolerance for short strings, Levenshtein distance is a good fit. If you want document overlap, token-based approaches such as Jaccard or cosine similarity are often better.

Use Levenshtein Distance for Short String Matching

Levenshtein distance counts the minimum number of insertions, deletions, and substitutions needed to turn one string into another.

javascript
1function levenshtein(a, b) {
2  const dp = Array.from({ length: a.length + 1 }, () =>
3    Array(b.length + 1).fill(0)
4  );
5
6  for (let i = 0; i <= a.length; i++) dp[i][0] = i;
7  for (let j = 0; j <= b.length; j++) dp[0][j] = j;
8
9  for (let i = 1; i <= a.length; i++) {
10    for (let j = 1; j <= b.length; j++) {
11      const cost = a[i - 1] === b[j - 1] ? 0 : 1;
12      dp[i][j] = Math.min(
13        dp[i - 1][j] + 1,
14        dp[i][j - 1] + 1,
15        dp[i - 1][j - 1] + cost
16      );
17    }
18  }
19
20  return dp[a.length][b.length];
21}
22
23console.log(levenshtein("kitten", "sitting")); // 3

This is useful for spell checking, matching names, or fuzzy search on short fields.

Normalize Distance into a Similarity Score

Raw edit distance is sometimes less convenient than a normalized score between 0 and 1.

javascript
1function levenshteinSimilarity(a, b) {
2  const distance = levenshtein(a, b);
3  const maxLen = Math.max(a.length, b.length);
4  return maxLen === 0 ? 1 : 1 - distance / maxLen;
5}
6
7console.log(levenshteinSimilarity("hello", "hallo"));

That makes it easier to define thresholds such as "consider two strings similar if the score is above 0.8."

Use Jaccard Similarity for Token Overlap

For longer text where word overlap matters more than character edits, Jaccard similarity is a strong baseline.

javascript
1function tokenize(text) {
2  return new Set(
3    text.toLowerCase().split(/\W+/).filter(Boolean)
4  );
5}
6
7function jaccardSimilarity(a, b) {
8  const setA = tokenize(a);
9  const setB = tokenize(b);
10
11  const intersection = [...setA].filter(token => setB.has(token)).length;
12  const union = new Set([...setA, ...setB]).size;
13
14  return union === 0 ? 1 : intersection / union;
15}
16
17console.log(jaccardSimilarity("red apple pie", "apple pie recipe"));

Jaccard ignores token frequency and order, which is sometimes exactly what you want for tags, keyword overlap, or simple duplicate detection.

Choose by the Kind of Similarity You Mean

Ask what "similar" means for your problem:

  • same spelling with small typos
  • same words in a different order
  • same overall topic
  • same semantic meaning with different vocabulary

Character distance and token overlap solve different problems. If you choose the wrong one, the code may run perfectly and still produce useless rankings.

For semantic similarity, simple string algorithms may not be enough. At that point you move toward embeddings or language-model-based approaches, which is a different level of tooling entirely.

Common Pitfalls

  • Using Levenshtein distance for long documents where token overlap matters more than character edits.
  • Using Jaccard when word frequency or word order actually matters.
  • Comparing raw text without normalizing case, punctuation, or whitespace first.
  • Expecting one threshold value to work for every domain and string length.
  • Calling a lexical similarity algorithm "semantic" similarity when it only compares characters or tokens.

Summary

  • Choose the similarity algorithm based on the type of text and the meaning of "similar."
  • Levenshtein distance is strong for short fuzzy string matching.
  • Jaccard similarity is a useful baseline for token overlap.
  • Normalize and preprocess text before comparing it.
  • Start with a simple algorithm that matches the problem before moving to heavier NLP methods.

Course illustration
Course illustration

All Rights Reserved.