What diff Does
The diff tool computes the edit distance between two texts — the minimum number of insertions and deletions needed to transform text A into text B. It outputs these changes so you can see exactly what was added, removed, or moved.
diff operates on lines by default. Git diff uses the same algorithm but can diff word-by-word or character-by-character with --word-diff and --word-diff=color.
The LCS Algorithm
Most diff implementations are based on finding the Longest Common Subsequence (LCS) of the two texts. Lines in the LCS are unchanged; everything else is an insertion or deletion.
For example, given sequences A = [1, 2, 3, 4, 5] and B = [1, 3, 4, 6, 5], the LCS is [1, 3, 4, 5]. Lines 2 was deleted, 6 was inserted.
Myers Diff Algorithm
Eugene Myers' 1986 algorithm is the one used in Git and most modern diff tools. It finds the shortest edit script (minimum edits) in O(ND) time, where N is the total length and D is the edit distance. For files that are mostly similar (low D), it is very fast.
Git also uses the "patience diff" and "histogram diff" algorithms as alternatives. Histogram diff often produces more readable diffs for refactored code by anchoring to unique lines.
# Git diff algorithms
git diff --diff-algorithm=myers # default
git diff --diff-algorithm=patience # better for moved code
git diff --diff-algorithm=histogram # best for large refactors
Reading Unified Diff Output
--- a/config.js ← original file
+++ b/config.js ← modified file
@@ -10,7 +10,8 @@ ← hunk header: original line 10, 7 lines; new line 10, 8 lines
const timeout = 5000;
-const retries = 3; ← deleted line
+const retries = 5; ← added line
+const backoff = "exponential"; ← added line
module.exports = { timeout, retries };
Lines starting with - were removed; lines starting with + were added; lines with a space are context (unchanged, shown for reference).
Three-Way Merge
Git merge uses a three-way diff: original file (common ancestor), your changes, their changes. Lines changed in only one branch are merged automatically; lines changed in both create a conflict:
<<<<<<< HEAD
const retries = 5; ← your change
=======
const retries = 10; ← their change
>>>>>>> feature/increase-retries
Diffing in JavaScript
// diff library
import Diff from "diff";
const changes = Diff.diffLines(oldText, newText);
changes.forEach(part => {
if (part.added) console.log("+ " + part.value);
if (part.removed) console.log("- " + part.value);
else console.log(" " + part.value);
});
Compare Text Instantly
Use ToolsVito's Diff Checker to compare two texts with highlighted additions and deletions — side-by-side and unified views, in your browser.