Skip to content

Compare buffer items with strict equality - #97

Merged
bhousel merged 1 commit into
mainfrom
strict-equality
Nov 12, 2026
Merged

bhousel merged 1 commit into
mainfrom
strict-equality

Conversation

@bhousel

@bhousel bhousel commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

⚠️ This changes results for some inputs. Arrays of strings, which is how most callers use the library, give identical output.

The problem

LCS built its lookup table with item values as object keys, so items were matched by their string form:

diffComm([{ a: 1 }], [{ b: 2 }]);  // [{ common: [{ a: 1 }] }], so any two objects "match"
diffComm([1], ['1']);              // [{ common: [1] }]

In a merge this can lose a change without reporting a conflict:

const x = { x: 1 }, y = { y: 2 }, z = { z: 3 };
diff3Merge([x, y], [x], [z]);
// before: [{ ok: [x, y] }], so b's replacement of x with z disappears
// after:  [{ conflict: { a: [x, y], o: [x], b: [z], ... } }]

It also disagreed with the rest of the code. isFalseConflict and the common-prefix trimming in LCS already compare with ===, and the declarations advertise generic T[] buffers.

The fix

The table is now a Map, which matches keys like ===: objects only match themselves, and 1 no longer matches '1'. The one difference from === is that NaN matches NaN. A Map also makes the prototype-less object workaround for items like constructor and __proto__ unnecessary. That regression test still passes.

The README's API Reference now says how items are compared.

Behavior change

Results change only when the buffers contain:

  • objects or arrays, which now match only themselves. Before, every object matched every other object.
  • values of different types with the same string form, such as 1 and '1', or null and 'null', which no longer match.

This probably needs a changelog entry marked ⚠️. It's arguably a bug fix, but anyone relying on numbers matching their string forms will see different results.

Verification

  • For string inputs, every exported function gives identical output to main on 330k seeded random cases plus all the benchmark workloads.
  • New tests in LCS.test.js and diff3Merge.test.js fail on main and pass here.

Performance

Workload Bun: before Bun: after Node: before Node: after
50k-line merge, diff3Merge 13.8 ms 6.8 ms 13.2 ms 10.1 ms
50k-line merge, mergeDigIn 9.9 ms 6.4 ms 23.7 ms 11.0 ms
200k unique lines, many hunks, diff3MergeRegions 36 ms 48 ms 141 ms 68 ms
600k lines, diff3Merge 12.5 ms 11.6 ms 39.9 ms 39.2 ms

The other workloads are within noise. On Node (and Chrome), the Map is faster across the board. On Bun, one case got slower: very large inputs where nearly every item is unique (36 → 48 ms for 200k lines). Bun's Map isn't slow there, since it now matches Node. Bun was just unusually fast with the plain-object version. Every workload is still far faster than before #95, so this PR keeps the simple Map rather than adding a separate lookup path for strings.

🤖 Generated with Claude Code

`LCS` looked up items as object keys, so they were matched by their string
form: all objects matched each other, and `1` matched `'1'`. In a merge this
could drop a change without reporting a conflict. Use a Map instead, which
matches items like `===` (and also handles `constructor`, `__proto__` etc.).

Output for string inputs is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bhousel
bhousel merged commit 911a272 into main Nov 12, 2026
1 check passed
@bhousel
bhousel deleted the strict-equality branch November 12, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant