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.
Keep terminators exact
A diff that keeps each line terminator verbatim can reconstruct both sides exactly; normalising line endings is a comparison option, not a rewrite.
03Limitations
Limitations
A text diff is positional: it does not understand a language, a schema, or an intent. Reordered blocks and reformatted JSON read as large changes even when the meaning is unchanged.
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
- An O(ND) Difference Algorithm and Its Variations (Myers, 1986) — ACM
- GNU diffutils manual — Free Software Foundation
- POSIX diff utility — The Open Group
Test this in your browser
The tool runs entirely in your browser; the input you use never leaves it.
About the author
Gampangin builds and maintains free, browser-based developer tools and the documentation that makes them trustworthy. The organization is accountable for every page published on this site.