Repository navigation
Compare buffer items with strict equality - #97
Merged
Merged
Conversation
`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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
LCSbuilt its lookup table with item values as object keys, so items were matched by their string form:In a merge this can lose a change without reporting a conflict:
It also disagreed with the rest of the code.
isFalseConflictand the common-prefix trimming inLCSalready compare with===, and the declarations advertise genericT[]buffers.The fix
The table is now a
Map, which matches keys like===: objects only match themselves, and1no longer matches'1'. The one difference from===is thatNaNmatchesNaN. AMapalso makes the prototype-less object workaround for items likeconstructorand__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:
1and'1', ornulland'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
mainon 330k seeded random cases plus all the benchmark workloads.LCS.test.jsanddiff3Merge.test.jsfail onmainand pass here.Performance
diff3MergemergeDigIndiff3MergeRegionsdiff3MergeThe other workloads are within noise. On Node (and Chrome), the
Mapis 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'sMapisn'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 simpleMaprather than adding a separate lookup path for strings.🤖 Generated with Claude Code