How a diff works, in plain English
A grocery list on the fridge and the list in someone’s pocket are not identical. The pocket list says “bread, butter, lemons, coffee.” The fridge list says “bread, salted butter, lemons, coffee, matches.” You can see the edit without a lecture: “salted” appeared, and “matches” appeared. A diff is that observation, done with a rule a computer can follow when the lists are longer than a fridge door and you do not trust your eyes.
The rule does not know that salted butter is a kind of butter. It knows which pieces occur in both lists and which pieces would have to be crossed off or written in to turn one list into the other. The pieces are whatever you decided to compare. On PasteDiff they are words or they are characters, depending on the mode. The idea is the same at either size.
An edit script, not a verdict
The output of a diff is an edit script: a sequence of “keep this,” “remove this,” and “insert this.” Applied to the left text, the removals and insertions produce the right text. Applied in reverse, they turn the right text back into the left. Nothing in the script says the right side is better. “Matches” might be a mistake. The script only says it is present on one side.
There are many scripts that work. You could delete the entire fridge list and insert the entire pocket list. That script is correct and useless, the way reprinting a page is a correct way to fix a typo. A useful script is a short one: keep “bread,” note that “butter” became “salted butter” or that “salted ” was inserted, keep “lemons” and “coffee,” insert “matches.” Short scripts are what the eye can check. The usual algorithms search for a short script. They do not search for the script a human editor would have described in the margin. Those are often the same and sometimes not. A human might say “you specified the butter.” The script says certain words were inserted. You still do the reading.
Shared beginnings and endings are free
If both lists start with “bread” and end with “coffee,” those pieces do not need a search. They match. A practical comparison peels off the shared start and the shared end, then spends its effort on the middle, where “butter, lemons” became “salted butter, lemons” and “matches” showed up. You see this in the highlighter as plain text around a marked phrase. The plain text was not ignored out of kindness. It was equal, so the edit script has nothing to say about it.
That is also why a change at the very start and a change at the very end still leave the middle unmarked if the middle really matches. The shared middle is found while the algorithm walks the two texts. It is not only the first and last words. Two copies of a letter that differ in the greeting and the sign-off should not light up the paragraphs you did not touch. When they do, the middle is not actually the same: a line ending, a hyphen, or a replaced word is in there. Line endings are a common reason a “small” edit paints a whole page.
The search, without the formula
Picture two strings of beads. You want the fewest bead-level mistakes that turn one string into the other: skip a bead that is on both strings, drop a bead, or add a bead. You can explore this recklessly, trying every possible mistake, and you will get a short script eventually. You will also take forever once the strings are long.
The methods used in real editors, including the approach PasteDiff uses, are more careful. They look for a short script first. A script with one insertion is tested before a script with fifty. Each step asks whether the next beads already match, in which case you can skip ahead at no cost, or whether you need another insertion or deletion. The first time you reach the end of both strings, you have a short script, because you have not been willing to consider longer ones yet. That idea is associated with Eugene Myers, whose 1986 paper describes an algorithm programmers still implement. You do not need the paper to use the result. You need to know that the marks you see are one short way to explain the change, not the only way, and not a summary written by someone who understood the grocery list.
Word mode runs this on words and on the gaps between them. Character mode runs it on graphemes. The algorithm does not change. The beads change. That is why the same two sentences can look like a neat clause edit in one mode and like a scatter of letters in the other. Choosing word or character highlighting is the practical consequence.
When the short script is still the whole text
If the pocket list and the fridge list share no items, the shortest honest script is “remove everything, insert everything.” Finer marks would be a fiction: there is no shared bead to keep. PasteDiff also stops looking once the middle of the text would require a very long script. Two unrelated pages should not freeze the browser while a search tries every alignment. You get one removed block and one added block. That is the script. It is readable. It tells you the texts are not an edit of one another.
A long shared text with a few replaced words stays in the fine-grained path, because the script is short even when the document is long. Length is not the problem. Difference is the problem. Comparing a chapter with a revision of that chapter is cheap if you kept most of the sentences. Comparing a chapter with a different chapter is a block highlight, and it should be.
What this means when you click Compare
The boxes are not compared while you type. Counts update immediately because counting does not need a script. The marks appear when you click Compare, and they are cleared when you type again so you are not staring at highlights for a sentence you have already changed. The left column is the first text with removals marked. The right column is the second text with additions marked. Text the script kept is unmarked. A change that is only whitespace is drawn so a deleted space does not look like a glitch in the paint.
If you want to see the script on the grocery list, paste the two versions and click Compare in word mode. You should see “salted” and “matches” as additions, and the rest plain, unless a space or a line break differs. If the whole list lights up, the beads are not the words you think they are. Look for a hidden character, clean it, and compare again. The algorithm is doing the simple thing. The paste is not always the list you remember writing.