toolhq.io

All posts
6 min readby Jameel Haider

How text diff works: line, word, and character comparisons

Comparing two pieces of text seems trivial until you try to do it well. A good diff does not just say "these are different," it shows the smallest set of changes that turns one version into the other. That is a real computation, and understanding it makes diffs easier to read.

What a diff is really computing

Under the hood, most diff tools solve a version of the longest common subsequence problem: find the largest amount of content the two texts share, in order, then everything outside that shared core is what changed. Framing it this way is why a good diff highlights a small inserted sentence rather than marking everything after it as different. The goal is the minimal, most readable set of edits.

The algorithm most tools actually use is Myers diff, published in 1986 and still the default in Git. It finds the shortest edit script, the fewest insertions and deletions, by searching a grid of the two inputs for the cheapest path. You do not need the internals to use a diff, but the consequence matters: a diff optimizes for the fewest edits, not necessarily the edits a human would have made.

Where the minimal-edit goal surprises you

Because the algorithm only inserts and deletes, it has no concept of a moved block. Cut a paragraph from the top of a file and paste it at the bottom, and a plain diff shows the paragraph deleted in one place and added in another, rather than "moved." It is technically the smallest edit script, but it is not how you think about the change. Some tools layer move detection on top; the underlying algorithm does not provide it.

Whitespace is the other common surprise. By default most diffs treat a changed indentation or a trailing space as a real change, which is why reformatting a file produces an enormous, meaningless diff. Most tools offer an "ignore whitespace" mode precisely to cut through that noise.

Line, word, and character granularity

  • Line diff treats each line as a unit. It is ideal for code and config, where changes are naturally line-based, and it is what version control shows.
  • Word diff compares word by word. For prose, where a sentence is edited mid-line, this pinpoints the changed words instead of flagging the whole line.
  • Character diff is the finest grain, useful for spotting a single changed digit or a typo.

Picking the right granularity is most of the battle. A line diff of edited prose is noisy; a character diff of code is overwhelming. A good rule: code and config use line diff, edited paragraphs use word diff, and you reach for character diff only when hunting one wrong symbol.

Reading a unified diff

The unified format, the one you see in code review, marks removed lines with -, added lines with +, and surrounds them with a few unchanged context lines:

@@ -4,3 +4,3 @@ function total(items) {
-  return items.reduce((a, b) => a + b)
+  return items.reduce((a, b) => a + b, 0)
 }

The @@ header gives the line numbers each hunk affects: -4,3 means three lines starting at line 4 in the old file, +4,3 the same in the new. Once you read - as "old" and + as "new," the whole format becomes obvious, and the surrounding context lines (no prefix) are there only to help you locate the change.

The Diff Checker compares two texts locally in your browser and highlights exactly what changed. For counting and analyzing a single text rather than comparing two, see the text statistics tool.