toolgarden.xyz
中文
diff algorithmtext comparisonadditionsdeveloper tools

How Text Diff Algorithms Find Additions, Deletions, and Changes

Diff algorithms look for shared subsequences between two texts, then mark the remaining parts as additions, deletions, or changes.

ToolGarden tools prioritize browser-local processing, so files and text do not need to be uploaded to a server.

Published July 2, 2026Updated August 3, 20267 min readBy ToolGarden

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 receipt

A 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

  1. Split text into lines, words, or characters.
  2. Find common parts that can be aligned.
  3. Mark gaps between common parts as additions or deletions.
  4. Adjacent deletions and additions can be displayed as modifications.
  5. Render the result as a highlighted view.

Line, Word, and Character Diff

GranularityBest forTrait
Line-levelLogs, configs, listsFast and easy to scan
Word-levelCopy, prose, sentencesShows changed words inside a line
Character-levelShort strings and identifiersPrecise but noisy for long text
StructuredJSON and object dataMore 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.

Frequently asked questions

Q.I changed only one character, but the diff shows the whole line as different; why?

This is normal for line-level diff. Line diff splits text at line breaks and compares whole lines, so any single-character change marks the entire line as deleted-plus-added. To see the exact character change, switch to word-level or character-level diff. Word diff splits the line into tokens and only highlights changed words. Character diff is even finer and shows individual letter changes. ToolGarden Text Diff supports switching granularity. Use line diff for code and configs, word diff for prose and copy.

Q.Why does diff show many delete+add pairs instead of a modification?

That is how classic diff algorithms (Myers, LCS variants) work; they only emit insertions and deletions, and the modification view is a rendering hint that merges adjacent delete+add pairs. If the two texts share very little in common between differing chunks, the algorithm cannot find long enough common subsequences and marks big blocks as pure delete-and-add. Try adjusting granularity (word or character), or align the paragraph order between the two versions before diffing, which reduces false-positive differences.

Q.Why should I use a JSON diff instead of a text diff for JSON data?

JSON is structured data, so formatting, indentation, and key order can change without altering meaning. A text diff reports all those cosmetic differences as real changes; a reformatted JSON file lights up entirely under text diff. A JSON diff parses both sides into object trees and compares by field path like user.address.city, ignoring format and key order. What you see then is a true data change: a field went from A to B, a new field appeared, an array grew. For API regression or data reconciliation, JSON diff is far more efficient.

Q.Can diff tools understand semantic changes like variable renaming?

Standard diff cannot. Classic algorithms only compare characters or lines and do not understand syntax. Renaming userName to user_name shows up as a delete of userName plus an insert of user_name, with no signal that it is the same variable. For semantic comparison, use specialized tools: GitHub semantic diff, JetBrains structural diff, or AST diff tools like gumtree and difftastic parse code into syntax trees before comparing. Standard diff is enough for daily coding; large-scale renames and refactors benefit from the smarter tools.

Q.Diffing two large files is slow or hangs; can it be optimized?

Diff has worst-case O(N×M) complexity, so two files with tens of thousands of lines can explode. Options: manually split by section and diff only changed parts; drop word or character granularity and use line diff; use command-line diff or git diff, which are heavily optimized for large files; or search for the specific area of interest and diff only that region. Browser-based diff typically handles files up to a few thousand lines well, and larger files are better served by desktop tools.