Guide

Diffing & Comparing Files Explained

How diffs work — line vs word diffs, the LCS/Myers algorithm, unified diff format, and why comparing JSON/YAML/CSV needs structural normalisation first.

Last updated: 26 July 2026

A diff (short for “difference”) is a compact description of what changed between two versions of some text or data — what was added, what was removed, and what stayed the same. Diffs power code review, version control, config auditing and data reconciliation. Understanding how they work helps you read them faster and pick the right kind of comparison for the job.

What a diff actually computes

Given an “old” and a “new” version, a diff finds the smallest set of edits that turns one into the other. To do that, it first works out which parts are unchanged — the largest chunk common to both sides — and treats everything else as either an insertion or a deletion.

Diffs come in different granularities:

The idea behind the algorithm: LCS

Most diff tools are built on the Longest Common Subsequence (LCS). A subsequence keeps items in order but may skip some — so the LCS of two files is the longest ordered set of lines (or characters) that appears in both. Whatever is not in the LCS is exactly what changed: items present only in the old version are deletions, and items present only in the new version are insertions.

For example, the LCS of A B C D and A X C D is A C D. Everything else (B removed, X added) is the diff.

The classic Myers diff algorithm (Eugene Myers, 1986) is the efficient way to find this. Rather than exhaustively comparing everything, it searches for the shortest edit script — the fewest insertions and deletions — by finding the longest common subsequence quickly. Git, Mercurial and most editors use Myers (or a close variant) under the hood. You don’t need the maths to use a diff, but the key intuition is simple: the tool keeps the biggest common part and reports the rest as changes.

Unified diff format

The most common way to display a diff is the unified format, the same one git diff and patches use:

@@ -1,4 +1,4 @@
 name: web
-replicas: 2
+replicas: 3
 image: nginx
-port: 80
+port: 8080

Reading it:

A file’s diff is a series of these hunks, each covering one region of change with a little surrounding context.

Text diff vs structural (semantic) diff

A plain text diff compares bytes and lines. That is perfect for code, but it produces misleading noise for data formats like JSON, YAML and CSV, because those formats allow the same data to be written many different ways:

A line diff flags all of these as differences even though nothing meaningful changed. A structural (semantic) diff avoids this by normalising first:

  1. Parse each side into its data model (objects, arrays, values).
  2. Re-serialise both with identical formatting.
  3. Sort keys (and, where order is irrelevant, sort arrays or rows) so ordering differences vanish.
  4. Then compare the normalised results.

After normalisation, only genuine value changes remain — a key added, a value edited, an element removed. This is what you want when reconciling two config files or two API responses.

Common uses

Tips for cleaner diffs

Text diff vs structural diff at a glance

Aspect Text diff Structural diff
Compares Raw lines / characters Parsed data model
Key / field re-ordering Shows as a change Ignored (keys sorted)
Whitespace & indentation Shows as a change Ignored (re-serialised)
Formatting (quotes, trailing commas) Shows as a change Ignored
Real value edits Caught Caught
Invalid / malformed input Still diffs it Fails to parse — must be valid first
Best for Code, prose, plain text JSON, YAML, CSV, config, API responses

The short version: a text diff catches everything including cosmetic noise; a structural diff catches only meaningful data changes but needs parseable input.

Work with diffs in your browser

Every CodingSetu diff and compare tool runs entirely client-side — your files are never uploaded. Compare plain text, code, JSON, YAML and CSV, with line, word and structural modes.

References

Try it: open the diff tools to compare text, JSON, YAML and CSV right in your browser.

Frequently asked questions

Why do my JSON files show as different when the data is the same?

A plain text diff compares characters and lines, so re-ordered keys, extra whitespace, different indentation, or trailing commas all look like changes even when the underlying data is identical. Use a structural (semantic) diff that parses both sides, sorts keys and re-serialises them, so only real value changes are reported.

What is the difference between a line diff and a word diff?

A line diff treats each whole line as a unit — if one character changes, the entire line is marked removed and re-added. A word or character diff looks inside the line and highlights only the exact tokens that changed. Line diffs are great for code; word/character diffs are better for prose or long lines.

Does the order of keys or rows matter when comparing?

For plain text diffs, yes — re-ordering lines or keys shows up as differences. For structural comparison it depends on the format. JSON and YAML objects are unordered, so keys should be sorted before comparing. Arrays and CSV rows are ordered by default, so sort them yourself only when the order genuinely does not matter.

Should I ignore whitespace when diffing?

Often, yes. Ignoring trailing whitespace, indentation and blank-line changes removes noise from reformatting so you see the substantive edits. But keep whitespace significant for whitespace-sensitive formats like Python, YAML, Makefiles or Markdown, where indentation changes meaning.

Is my data uploaded when I use the diff tools?

No. Every CodingSetu diff and compare tool runs entirely in your browser. Your files never leave your device and are never sent to a server.

Related tools