Every time you run git diff, submit a pull request on GitHub, or review changes in VS Code, an algorithm is silently calculating the exact difference between two versions of your text.
Diffing seems simple at first glance: compare line 1 to line 1, line 2 to line 2. But what happens when someone inserts a single line at the very top of a 5,000-line file? A naive comparison would declare that every single line in the entire file has changed!
To solve this, computer scientists developed algorithms based on the Longest Common Subsequence (LCS) problem. Here is how modern diff engines work under the hood.
1. The Core Problem: Finding the Shortest Edit Script
The goal of a diff algorithm is to transform sequence $A$ into sequence $B$ using the minimum number of edit operations (insertions and deletions). This minimal sequence of edits is called the Shortest Edit Script (SES).
Consider two strings:
- String $A$:
ABCABBA - String $B$:
CBABAC
If we find the longest sequence of characters that appears in both strings in the same order (the Longest Common Subsequence), the characters not in the LCS are our edits:
- LCS:
CBBA - Deletions from $A$: remove
A, remove firstB - Insertions into $B$: add
C, addA
Finding the LCS is mathematically dual to finding the Shortest Edit Script: $$\text{Length of SES} = \text{Length}(A) + \text{Length}(B) - 2 \times \text{Length}(\text{LCS})$$
2. Eugene Myers' Diff Algorithm (The Engine of Git)
In 1986, Eugene W. Myers published a landmark paper titled "An $O(ND)$ Difference Algorithm and Its Variations". It remains the default diff algorithm in Git today.
How Myers Graph Works
Myers visualized the problem as finding the shortest path on an edit grid:
- Moving right ($x o x+1$) represents deleting a character from file $A$.
- Moving down ($y o y+1$) represents inserting a character into file $B$.
- Moving diagonally ($x+1, y+1$) represents a match (both files have the same line), which costs zero edit distance!
By treating diagonal moves as "free rides", the algorithm uses a breadth-first search along diagonals $k = x - y$. It finds the shortest path in $O(ND)$ time, where $N$ is the sum of file lengths and $D$ is the number of differences. For files with few differences (common in source code), Myers is blindingly fast.
3. Split View vs. Unified View: Which Should You Use?
Modern diff tools present changes in two distinct visual layouts:
Unified (Inline) View
Popularized by GNU diff -u and GitHub PR comments, unified view displays all lines in a single vertical stream.
- Modified lines are marked with
-(red/removed) directly above+(green/added). - Best for: Small edits, code reviews on laptop screens, and patch files.
Split (Side-by-Side) View
Displays the original file on the left and the modified file on the right, with synchronized scrolling.
- Unchanged lines align horizontally. Removed text is highlighted on the left, added text on the right.
- Best for: Large architectural refactors, comparing database records, or proofreading legal contracts.
4. Why You Should Never Paste Sensitive Code into Random Cloud Diff Tools
Many free online diff websites send your pasted text straight to their web servers for backend processing via Python or PHP diff libraries.
If you are comparing:
- Production configuration files containing API keys or database passwords,
- Proprietary algorithms or unreleased intellectual property,
- Confidential contracts, medical logs, or customer PII,
uploading them to an unverified third-party server creates significant data leak exposure and violates compliance frameworks like GDPR, HIPAA, and SOC 2.
Always verify that your diff tool executes 100% client-side in your browser.
5. Instant, Private Diff Checking with KitWharf
The KitWharf Text & Code Diff Checker runs an optimized LCS diff engine entirely in JavaScript inside your browser.
- Split & Unified Views: Switch between two-column side-by-side comparison and inline diff with one click.
- Character & Word Diffing: Highlights the exact words and punctuation changed within modified lines.
- Smart Filters: Ignore whitespace changes and ignore case toggles to eliminate noisy formatting diffs.
- File Upload & Drag-and-Drop: Load text or code files directly from your computer without uploading them anywhere.
- Zero Server Storage: Your data never leaves your device RAM.