Text Diff Checker & Code Comparison
Compare two text files, source code snippets, or document revisions with side-by-side and unified diff views, line numbers, and similarity metrics. 100% private and client-side.
How Diff Algorithms Work: The Longest Common Subsequence (LCS)
Automated text and code comparison rests mathematically on the Longest Common Subsequence (LCS) problem, revolutionized in 1986 by computer scientist Eugene W. Myers in his landmark paper An O(ND) Difference Algorithm and Its Variations:
Longest Common Subsequence (LCS): The longest sequence of tokens (characters, words, or lines) appearing in both documents in identical relative order without requiring contiguity.Sørensen–Dice Similarity Formula: Similarity = [ 2 × |LCS| ] ÷ [ |Original| + |Modified| ] × 100%
The algorithm models two text files as a directed grid graph where diagonal traversals represent identical matching lines, horizontal edges represent insertions (additions), and vertical edges represent deletions. Finding the optimal "diff" is mathematically equivalent to identifying the shortest edit path across the coordinate graph.
Dynamic Programming LCS vs. Myers O(ND) Algorithm
Computer science utilizes two primary approaches for executing diff scripts:
| Algorithmic Approach | Time Complexity | Space Complexity | Primary Engineering Deployment |
|---|---|---|---|
| Dynamic Programming (2D Matrix) | $O(M \times N)$ | $O(M \times N)$ | In-browser client-side scripts; optimal for documents under 2,000 lines with zero memory overhead. |
| Myers Greedy Traversal (Linear Space) | $O(N \times D)$ (where $D$ is edit count) | $O(N)$ | POSIX command-line utilities, GNU diff, and core Git version control engines for massive codebases. |
Side-by-Side (Split) vs. Unified (Inline) Diff Views
Different review workflows benefit from distinct visual representations:
| Diff Layout Mode | Display Architecture | Optimal Real-World Application |
|---|---|---|
| Side-by-Side (Split) | Dual-column pane placing original text on the left and revised text on the right with synchronized line numbering. | Reviewing legal contracts, terms of service revisions, academic manuscripts, and complex code refactoring. |
| Unified (Inline) | Single-column linear stream displaying deletions directly above additions with + and - indicators. |
Terminal code reviews, GitHub commit pull requests, command-line patch files, and narrow mobile viewports. |
Anatomy of a Unified Diff Patch File (POSIX & Git Standards)
Standard version control patch files (RFC 3284 / POSIX) structure changes using standardized chunk headers:
--- a/src/calculate.js (Original File Header)+++ b/src/calculate.js (Modified File Header)@@ -14,6 +14,8 @@ (Hunk Header: Original line 14 for 6 lines, Modified line 14 for 8 lines) const basePrice = 100;-const taxRate = 0.05; (Deleted Line - Red)+const taxRate = 0.0825; (Added Line - Green)+const discount = 10; (Added Line - Green)
For network engineers comparing router configurations or firewall ACL rules across devices, audit CIDR blocks with our IPv4 Subnet Mask & CIDR Calculator.