The ability to compare two versions of text and see exactly what changed is fundamental to software development. Here's how diff algorithms work, how to use them in code, and when a browser-based tool beats the command line.
What is a diff?
A diff shows the changes between two texts: additions (lines only in the new version), deletions (lines only in the old version), and unchanged lines. The classic output format is:
``
+ new line
unchanged line
`
Lines starting with - exist only in the first version, + only in the second, and (space) are unchanged in both.
The Myers diff algorithm
The standard diff algorithm is Myers (1986), which finds the shortest edit script — the minimum number of character insertions and deletions needed to transform text A into text B. It's used by git diff, diff, and most diff libraries.
The algorithm works by finding the longest common subsequence (LCS) of the two texts. Lines not in the LCS are the changed lines.
A text diff checker applies this at the word level (not just line level), giving more precise highlighting of exactly which words changed within a paragraph.
Using diff in JavaScript
The diff npm package (jsdiff) is the most widely used JavaScript diff library:
<code>bash
<p>npm install diff</p>
</code>
`javascript
import { diffWords, diffLines, diffChars, createTwoFilesPatch } from 'diff';
// Word-level diff
const changes = diffWords('old text here', 'new content here');
changes.forEach(part => {
if (part.added) console.log(+ ${part.value});
else if (part.removed) console.log(- ${part.value});
else console.log( ${part.value});
});
// Line-level diff
const lineDiff = diffLines(file1Content, file2Content);
// Unified diff format (like git diff)
const patch = createTwoFilesPatch('file1.txt', 'file2.txt', old, newText);
console.log(patch);
`
Rendering colored diff in the terminal:
`javascript
import { diffWords } from 'diff';
const diff = diffWords(text1, text2);
let output = '';
diff.forEach(part => {
if (part.added) output += \x1b[32m${part.value}\x1b[0m; // green
else if (part.removed) output += \x1b[31m${part.value}\x1b[0m; // red
else output += part.value;
});
console.log(output);
`
Using diff in Python
`python
import difflib
text1 = "Hello World\nThis is a test\nHere is the content"
text2 = "Hello World\nThis is updated\nHere is the content"
Line-level diff
diff = list(difflib.unified_diff(
text1.splitlines(keepends=True),
text2.splitlines(keepends=True),
fromfile='old.txt',
tofile='new.txt'
))
print(''.join(diff))
Side-by-side HTML diff
differ = difflib.HtmlDiff()
html = differ.make_file(text1.splitlines(), text2.splitlines())
with open('diff.html', 'w') as f:
f.write(html)
Similarity ratio
ratio = difflib.SequenceMatcher(None, text1, text2).ratio()
print(f"Similarity: {ratio:.1%}")
Find close matches in a list
difflib.get_close_matches('colour', ['color', 'colour', 'colorful'])
→ ['colour', 'color']
`
Command-line diff
`bash
Basic file diff
diff old.txt new.txt
Unified format (like git diff, most readable)
diff -u old.txt new.txt
Side-by-side
diff -y old.txt new.txt
Ignore whitespace differences
diff -w old.txt new.txt
Ignore case
diff -i old.txt new.txt
Recursive (directory diff)
diff -r old_dir/ new_dir/
Only show files that differ (no content)
diff -rq old_dir/ new_dir/
`
Git diff
`bash
Unstaged changes
git diff
Staged changes (what's about to be committed)
git diff --staged
Between commits
git diff HEAD~1 HEAD
Between branches
git diff main..feature-branch
Word-level diff (color-coded in terminal)
git diff --word-diff=color
Stat only (no content)
git diff --stat
`
Using VS Code as git difftool:
<code>bash
<p>git config --global diff.tool vscode</p>
<p>git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'</p>
<p>git difftool file.txt</p>
</code>
When to use a browser tool vs command line
Use a browser diff tool when:
- You have raw text (not a file) to compare — pasting API responses, copied paragraphs, or content from different sources
- You need word-level highlighting (not just line-level) for prose and documentation
- You're comparing content from different sources (a PDF export vs. a Word doc)
- You don't have terminal access (reviewing on a mobile device or shared machine)
Use git diff when:
- You're working with files in a git repository
- You need to compare branches or commits
- You want the diff integrated into your editor
Use diff command when:
- Comparing files outside of git
- Scripting file comparisons in CI/CD
- Need recursive directory comparison
Diffing structured data
For JSON: format both objects with sorted keys before diffing (otherwise key order changes produce noise):
<code>bash
<h1>Sort JSON keys before diffing</h1>
<p>python3 -c "import json, sys; print(json.dumps(json.load(sys.stdin), indent=2, sort_keys=True))" < file1.json > a.json</p>
<p>python3 -c "import json, sys; print(json.dumps(json.load(sys.stdin), indent=2, sort_keys=True))" < file2.json > b.json</p>
<p>diff a.json b.json</p>
</code>
For CSV: sort both files before diffing to account for row order changes:
<code>bash
<p>sort file1.csv > a.csv</p>
<p>sort file2.csv > b.csv</p>
<p>diff a.csv b.csv</p>
</code>
For YAML:
<code>bash
<h1>Convert to JSON for canonical comparison</h1>
<p>python3 -c "import sys, json, yaml; print(json.dumps(yaml.safe_load(sys.stdin), indent=2, sort_keys=True))" < file1.yaml > a.json</p>
<h1>...then diff the JSON files</h1>
</code>
Diffing is a solved problem with excellent tooling at every level — from single keystrokes in a browser to complex programmatic comparison pipelines. The key is matching the tool to the context: browser for ad-hoc text comparisons, git for code, diff` for files, and libraries for application-level diffing.
Originally published at https://snappytools.app/text-diff-checker/