How a diff checker decides what changed
How the text compare tool finds added, removed and changed lines with Myers' diff algorithm, pairs edited lines, marks the changed words, and when to ignore case or spaces.
Last reviewed · 1,251 words
In short
- A diff lists the fewest lines to remove and add that turn the original into the changed text. This page uses Myers' algorithm, the method git uses by default.
- An edited line is shown as one changed line with the exact words marked, not as a removal and an addition, when the two versions share at least 40% of their words.
- Ignore case and Ignore extra spaces make lines that differ only in those ways count as the same, which helps with text copied from PDFs and emails.
- It is fast. Two 5,000-line texts with 50 edited lines were compared in 7 milliseconds in our test.
- Nothing is uploaded. Both texts are compared in this browser tab.
Two drafts of a rent agreement, two versions of an essay, the code before and after a fix, two copies of a price list: reading them side by side, the eye misses things. A diff checker reads both and lists every difference, line by line, with the changed words marked. This page does that in your browser. Here is how it decides what changed, and how to read the result.
How to compare two texts
- Paste the original in the first box and the changed version in the second.
- Read the result on the right. Red lines were in the original only, green lines are in the changed text only, and a red line followed by a green one is a line that was edited, with the exact words that changed marked darker.
- Use the options if needed: ignore case, ignore extra spaces, show only the lines that differ, or switch to the side-by-side view.
Copy diff copies the whole result as text, with - before removed lines and + before added lines, the format developers paste into chats and tickets.
What a diff actually computes
A diff answers one question: what is the shortest list of lines to remove from the original and lines to add, in order, so that it becomes the changed text? Lines that are in neither list are the same in both.
Shortest is the important word. There are always many ways to describe the change between two texts; the trivial one is "remove every line, add every line". The shortest list matches up as many unchanged lines as possible, which is what a reader means by "what changed".
The method this page uses was published by Eugene Myers in 1986 and is the default in git. It searches outwards from zero edits, one edit, two edits, and stops at the first number of edits that reaches the end of both texts, so it is fast when the texts are mostly alike, which is almost always the case. In our test, two 5,000-line documents differing in 50 lines were compared in 7 milliseconds.
Very different texts are the slow case. If two texts need more than 4,000 edits, the page stops searching and shows the different middle as removed and then added, with a note saying so. Texts that different are rarely compared line by line anyway.
Reading the example
The page opens with two drafts of a rent agreement. The diff reports 4 changed lines, 1 added, 1 removed and 1 the same:
| Line in draft 1 | Line in draft 2 | Shown as |
|---|---|---|
| Rent agreement, draft 1 | Rent agreement, draft 2 | changed: "1" became "2" |
| Monthly rent: Rs 15,000 | Monthly rent: Rs 16,500 | changed: "15" became "16" and "000" became "500" |
| Security deposit: two months | Security deposit: two months | the same |
| Lock-in period: 11 months | removed | |
| Notice period: one month | Notice period: two months | changed: "one month" became "two months" |
| Maintenance paid by tenant | Maintenance paid by owner | changed: "tenant" became "owner" |
| Painting at the end of the lease | added |
The lock-in line was deleted outright, and the lines after it still line up with their new versions. A simpler checker that pairs removed and added lines by position would have shown "Lock-in period" as edited into "Notice period: two months", and every line after it as edited into the wrong neighbour. This page pairs lines by how alike they are instead.
The rent shows how the word marking works: "Rs 15,000" and "Rs 16,500" share "Rs" and the comma, so only the two numbers either side of the comma are marked.
How edited lines are paired
When a block of lines is removed and another block added in the same place, the page decides which removed line became which added line. For each possible pair it measures how many words the two lines share, as a share of all their words; a pair counts as one edited line if they share at least 40%. It then picks the pairing, in order, with the most shared words overall.
Notice period: one month and Notice period: two months share three of their five words each, 60%, so they pair. Lock-in period: 11 months shares fewer with every candidate than the true pairs do, so it stays a plain removal.
Inside a paired line, the same shortest-edit method runs again on words, spaces and punctuation, and the words that differ are marked. A space between two changed words is marked with them, so "one month" reads as one change.
Ignore case and ignore spaces
Ignore upper and lower case treats "GST" and "gst" as the same. Use it when one version went through a tool that changed capitals.
Ignore extra spaces and tabs treats any run of spaces, tabs or both as one space, and ignores spaces at the start and end of a line. Text copied from a PDF, a web page or an email often picks up double spaces and trailing spaces that nobody typed; without this option, those lines all show as changed.
Both options only change what counts as the same. The lines are always shown exactly as you pasted them.
Comparing code, JSON and lists
Code compares well as it is, one line per statement. Indentation changes show as changed lines unless you ignore spaces.
JSON should be formatted first. Minified JSON is one long line, and a diff of one line only says "it changed". Run both versions through the JSON Formatter so each key sits on its own line, then compare.
Lists such as price lists and attendance compare best when both are sorted the same way. A diff follows order: the same item moved from line 3 to line 30 shows as a removal and an addition.
Hindi and other scripts
Words in Hindi, Marathi, Tamil and other Indian scripts are compared whole, including their vowel signs, so a changed word is marked as a word rather than as broken letters. Mixed Hindi and English lines work the same way.
Side by side or inline
Inline, the default, lists the result as one column: each removed line in red with its original line number, each added line in green with its new line number, and unchanged lines with both. It reads well on a phone and when copying the diff.
Side by side puts the original on the left and the changed text on the right, row by row, with a grey gap opposite each line that exists on one side only. It is easier for long documents on a wide screen, where you want to see the old and new wording at the same height.
Show only the lines that differ hides the unchanged lines in either view, which turns a 300-line contract with three edits into a three-row summary.
What this tool assumes
- Texts are split into lines at line breaks; Windows, Mac and Unix line endings are all treated as one break.
- Lines are matched with Myers' shortest-edit algorithm, up to 4,000 edits; beyond that the differing middle is shown as removed then added.
- Removed and added lines in the same place are paired when they share at least 40% of their words, choosing the in-order pairing with the most shared words.
- Words, runs of spaces and single punctuation marks are the units compared inside a changed line.
- The texts stay in your browser tab and are not uploaded or saved.