tools / diff
diff/
Compare two texts line by line, with additions and deletions highlighted in a unified view.
Original
Changed
Differences
What is text comparison
Line-by-line text comparison looks for the fewest whole-line deletions and insertions that turn the original into the new text. This page computes it with the difference algorithm Myers published in 1986, up to 50000 lines on each side, and marks additions and deletions in one unified view.
How it works: number the lines, then find the shortest edit
- Both texts are split into lines at line breaks, and each distinct line content gets a number, so identical lines share a number and only numbers are compared from then on; "Ignore leading/trailing whitespace" and "Ignore case" apply before the numbering.
- Identical lines at both ends are matched directly, and only the differing middle part is computed.
- The middle part is searched from both ends towards the middle with the Myers algorithm; once a midpoint on a shortest edit path is found, it is split in two and each half is computed again, so memory grows only linearly with the number of lines.
- Within each run of consecutive changes, deletions are listed before insertions.
Example
Line 2 of the original, b, became B, and an extra line d was added at the end. In the output, - 2 b deletes line 2 of the original, and + 2 B and + 4 d are lines 2 and 4 of the new text: 2 lines added and 1 line removed in total.
- Input
- {"old":"a\nb\nc","new":"a\nB\nc\nd"}
- Output
- 2 b + 2 B + 4 d
Sources
- Wikipedia: diff
- E. W. Myers, An O(ND) Difference Algorithm and Its Variations, Algorithmica, 1986
FAQ
How do I read the two line-number columns and the + / - signs? Is there a side-by-side view?
Each row starts with two line numbers: the first is the line number in "Original" and the second is the line number in "Changed". Rows starting with - exist only in the original (removed), rows starting with + exist only in the new text (added), and rows with no sign are identical. The +N -N next to the "Differences" heading are the counts of added and removed lines. There's only this unified view, no side-by-side, and unchanged lines are all shown.
I changed one word in a line. Why is the whole line marked as changed?
The tool compares whole lines and doesn't highlight which words changed inside a line. If anything in a line changes, the old line is shown as removed and the new line as added (removed first). To see exactly what changed, split the line into several lines at punctuation or spaces and compare again. A paragraph that moved shows up as one deletion and one addition.
Additions and deletions are the wrong way round. What do I do?
Put the old version in "Original" and the new version in "Changed". If you pasted them the other way, click "⇄ Swap sides" and additions and deletions switch places.
How do I stop differences in spaces, indentation or line endings from counting?
Different line endings (Windows, Mac, Linux) never count as differences, and neither does one extra newline at the end of the text. For spaces and indentation at the start or end of a line, tick "Ignore leading/trailing whitespace". Spaces in the middle of a line, tabs versus spaces, and extra blank lines still count. "Ignore case" applies to the whole line.
How much text can it compare? Why does it say "Too many lines" or pause live comparison?
Each side can have at most 50,000 lines; beyond that you get a "Too many lines" message and no comparison. When the two sides together exceed 500,000 characters, it stops comparing as you type and you need to click "Compare". Only the first 20,000 result rows are displayed, with a single note for the rest; the +N -N counts at the top still include everything.
What does "Took longer than 3 s; some differences are shown as whole-block replacements" mean?
When two long texts differ a lot, finding the shortest set of changes can be slow. After 3 seconds, the part that isn't finished is shown as one block deleted and one block added. The result is still correct; it just may not be the shortest way to describe the change.
Is the result the same as git diff? Can I export a patch?
The approach is the same but the output isn't guaranteed to match. Both compare line by line with the Myers algorithm (git diff's default), but when several equally short sets of changes exist, the one chosen can differ. Here there are no @@ hunk headers, all unchanged lines are shown, and a patch can't be exported; when you select and copy the result, line numbers and the +/- signs aren't copied.
Is the code or text I compare uploaded?
No. The comparison runs in your browser, the page sends no requests with your content, and it doesn't store your input locally either.