DEV Community

Alon Vodovoz
Alon Vodovoz

Posted on AI-assisted

How a browser-only text diff works (and where it falls short)

I'm the maker of TextCompareLab, a free tool that compares two versions of a text in the browser. The texts are not uploaded. This post explains how the comparison works and what it can't do.

Line level

Both texts are split into lines. The tool first strips the lines the two texts share at the start and at the end, then finds the longest common subsequence of what remains with a dynamic-programming table. Lines that are in the first text only are marked removed, and lines in the second text only are marked added.

If the middle part is too big (more than 36 million table cells), the tool stops and shows a message instead of freezing the tab.

Word level

When a run of removed lines sits next to a run of added lines, they are paired up as modified lines. Inside each pair the same algorithm runs again, this time on tokens: whitespace, runs of letters and digits (Unicode-aware), and single punctuation marks. That is why only the changed words are highlighted rather than the whole line.

Options

"Ignore whitespace" collapses runs of whitespace into one space before comparing. "Ignore case" lowercases before comparing. The text you see is still your original.

Limits

  • It compares line by line. If you re-wrap a paragraph, the lines change and the tool shows them as changed.
  • It does not detect moved blocks. A paragraph moved elsewhere shows as removed in one place and added in another.
  • Very large inputs hit the size guard above.

Try it

https://textcomparelab.com/

I'm the author, so I'd like feedback: which kind of text do you compare most, and what is missing for it?

Top comments (0)