Levenshtein Edit Distance With Backtrace

Compute edit distance over Unicode code points and reconstruct insert, delete and substitute steps from the dynamic-programming matrix.

JavascriptData Integrity

Code

javascriptsnippet
57 lines
export function editDistance(source, target, options = {}) {
  if (typeof source !== "string" || typeof target !== "string") {
    throw new TypeError("source and target must be strings");
  }
  const { maxLength = 4000 } = options;
  const left = Array.from(source);
  const right = Array.from(target);
  if (left.length > maxLength || right.length > maxLength) {
    throw new RangeError(`input exceeds maxLength of ${maxLength} code points`);
  }
  const rows = left.length + 1;
  const columns = right.length + 1;
  const matrix = Array.from({ length: rows }, () => new Uint32Array(columns));
  for (let row = 0; row < rows; row += 1) matrix[row][0] = row;
  for (let column = 0; column < columns; column += 1) matrix[0][column] = column;
  for (let row = 1; row < rows; row += 1) {
    for (let column = 1; column < columns; column += 1) {
      const cost = left[row - 1] === right[column - 1] ? 0 : 1;
      matrix[row][column] = Math.min(
        matrix[row - 1][column] + 1,
        matrix[row][column - 1] + 1,
        matrix[row - 1][column - 1] + cost,
      );
    }
  }
  return {
    distance: matrix[rows - 1][columns - 1],
    operations: backtrace(left, right, matrix),
  };
}

function backtrace(left, right, matrix) {
  const operations = [];
  let row = left.length;
  let column = right.length;
  while (row > 0 || column > 0) {
    const diagonalCost = row > 0 && column > 0 && left[row - 1] === right[column - 1] ? 0 : 1;
    if (row > 0 && column > 0 && matrix[row][column] === matrix[row - 1][column - 1] + diagonalCost) {
      if (diagonalCost === 1) {
        operations.push({ type: "substitute", from: left[row - 1], to: right[column - 1] });
      }
      row -= 1;
      column -= 1;
    } else if (row > 0 && matrix[row][column] === matrix[row - 1][column] + 1) {
      operations.push({ type: "delete", value: left[row - 1] });
      row -= 1;
    } else {
      operations.push({ type: "insert", value: right[column - 1] });
      column -= 1;
    }
  }
  return operations.reverse();
}

console.log(editDistance("kitten", "sitting"));
console.log(editDistance("book", "back"));
console.log(editDistance("", "abc"));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

{
  distance: 3,
  operations: [
    { type: 'substitute', from: 'k', to: 's' },
    { type: 'substitute', from: 'e', to: 'i' },
    { type: 'insert', value: 'g' }
  ]
}
{
  distance: 2,
  operations: [
    { type: 'substitute', from: 'o', to: 'a' },
    { type: 'substitute', from: 'o', to: 'c' }
  ]
}
{
  distance: 3,
  operations: [
    { type: 'insert', value: 'a' },
    { type: 'insert', value: 'b' },
    { type: 'insert', value: 'c' }
  ]
}

What the code does

Count the functions editDistance and backtrace as the entire surface area of Levenshtein Edit Distance With Backtrace.

The core work runs in nested loops — outer for (let row = 0; row < rows; row += 1) matrix[row][0] = row;, inner for (let column = 0; column < columns; column += 1) matrix[0][column] = column;. Invalid states are rejected up front: it throws TypeError and RangeError instead of continuing with bad data.

The result comes back as an object carrying left, right.

What to watch for

  • The guard typeof source !== "string" || typeof target !== "string" short-circuits the flow with an early throw when its condition holds.

Applying it

Entry signature: export function editDistance(source, target, options = {})

Treat editDistance as the API: import it, and ignore the supporting code.

The call returns left, right — destructure what you need at the call site.

Statements that matter

for (let column = 0; column < columns; column += 1) matrix[0][column] = column;
throw new RangeError(`input exceeds maxLength of ${maxLength} code points`);
const matrix = Array.from({ length: rows }, () => new Uint32Array(columns));

Interactive Tools for This Topic

FAQ

Does `editDistance` modify its input?
No mutation happens — the function reads its input and returns a new value.
What does the snippet do with malformed data?
It throws `TypeError` and `RangeError` before any real work runs, via the guard `typeof source !== "string" || typeof target !== "string"` — handle it at the call site or let it propagate deliberately.
Does this require external packages?
No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. The file defines `editDistance`, `backtrace` at load time.