A diff algorithm does not understand meaning. It finds which parts of two sequences are shared and which parts changed.
In practice, put the older version on the left, the newer version on the right, and choose a granularity that fits the material. Line-level output locates changed passages quickly, while word-level highlighting shows exactly what changed inside a line. Used together, they preserve the overall structure without hiding a changed number, status, or phrase.
A reproducible example
Old version
Checkout success
Payment pending
Send email receipt
New version
Checkout success
Payment completed
Send email receiptA line diff reports the second line as one deletion and one addition. Word comparison then aligns the shared word Payment and highlights only pending versus completed. This is exactly the kind of unchanged line shape with a changed state that people miss while scanning a long log.
The Basic Idea
- Split text into lines, words, or characters.
- Find common parts that can be aligned.
- Mark gaps between common parts as additions or deletions.
- Adjacent deletions and additions can be displayed as modifications.
- Render the result as a highlighted view.
Line, Word, and Character Diff
| Granularity | Best for | Trait |
|---|---|---|
| Line-level | Logs, configs, lists | Fast and easy to scan |
| Word-level | Copy, prose, sentences | Shows changed words inside a line |
| Character-level | Short strings and identifiers | Precise but noisy for long text |
| Structured | JSON and object data | More accurate by field path |
Many diff implementations use longest common subsequence ideas or similar strategies: preserve shared content and explain changes with insertions and deletions.
Reducing differences that do not matter
- Normalize line endings and encodings first, so Windows CRLF and Unix LF do not make a whole passage look different.
- For logs, remove timestamps, request IDs, or random values that change on every line before comparing the useful payload.
- Use a structured JSON diff for JSON documents, where indentation and key order are not data changes.
- A moved block normally appears as a deletion at the old location and an insertion at the new one; that does not mean its content was rewritten.
- Split very large files by section or time range so browser calculation and human review both stay focused.
Treat the diff as review evidence, not a verdict
The algorithm proves that two text sequences differ, but not whether the change is correct. Configuration review still needs units and environment context, copy review needs the surrounding paragraph, and generated code still needs its normal checks. Pay particular attention to apparently tiny changes in version numbers, decimal points, minus signs, and permission values.