Guide

How Text Diffing Works

A plain-language guide to line, word, and character diffing: what a diff algorithm compares and why granularity changes the result.

A text diff finds the shortest set of edits that turns one text into another. The classic formulation is the shortest edit script: the fewest inserted and removed symbols between the two sequences.

01Granularity

  • Line diffing treats each line as one symbol; it is fast and matches how source files are reviewed.
  • Word diffing refines a changed line so only the words that moved are highlighted.
  • Character diffing refines further, to the individual characters, which is noisier but exact.

02Reading a diff

  • Equal lines are context shared by both sides.
  • Removed lines exist only in the original; added lines exist only in the changed text.
  • Paired removed and added rows form a change, which is what intra-line highlighting refines.

03Limitations

Frequently asked questions

Which algorithm does the Diff Checker use?
A line-level Myers (1986) shortest-edit-script comparison, refined to word or character level for paired changes, bounded by a documented edit-distance limit.
Why does reformatting JSON show as a big diff?
A text diff compares characters and lines, so indentation and key reordering are real differences to it. Use a semantic JSON comparison for that case.

Sources

  1. An O(ND) Difference Algorithm and Its Variations (Myers, 1986) — ACM
  2. GNU diffutils manual — Free Software Foundation
  3. POSIX diff utility — The Open Group