Skip to content

Compare array items without building their diff (default diff only) - #117

Closed
vicocotea wants to merge 1 commit into
chbrown:masterfrom
vicocotea:perf/array-item-equality
Closed

vicocotea wants to merge 1 commit into
chbrown:masterfrom
vicocotea:perf/array-item-equality

Conversation

@vicocotea

Copy link
Copy Markdown
Contributor

This is the "exit at the first inequality" idea @joshkel proposed in #103, split out as asked, without fast-deep-equal: no new dependency, and the current equality rules are kept (undefined keys, NaN, Dates), so the patches don't change.

With the default diff, diffArrays now compares items with a private equalAny instead of !diff(a, b, ptr).length, which built the whole nested diff only to check it was empty (the TODO from bb8c99b). A custom diff is still asked, so #15 is unaffected.

  • 2× to 20× faster createPatch, depending on the size and nesting of the array items (e.g. a 120-paragraph ProseMirror doc: 39 ms → 3.2 ms).
  • Same patches as 5.5.0 on 60,000 random inputs; the 5 new tests also pass on master.

The quadratic cost on long arrays remains; docmost/docmost#2689 has a banded (Ukkonen-style) version with identical patches in my tests, and I'm asking its author about upstreaming it.

With the default diff, diffArrays now checks whether two items are equal
with a dedicated equalAny instead of !diff(a, b, ptr).length, which built
the whole nested diff (paths included) only to check that it was empty.
equalAny follows the current rules of diffAny/diffObjects and stops at the
first difference. A custom diff is still asked, so chbrown#15 is unaffected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
chbrown added a commit that referenced this pull request Nov 21, 2026
With the default diff, diffArrays now checks whether two elements are
equal with isEqual, which gives the same answer as !diffAny(...).length
but stops at the first difference and builds no operations or pointers.
A custom diff is still called, since it may consider elements equal that
isEqual wouldn't (#15). equalAtomic now takes an equality callback, so
the Date/Map/Set rules are shared by diffAny and isEqual.

Based on the early-exit idea from #103 and indepedently matches
implementation in #117 (our Claudes think alike ;)).

Coded by Claude, who lacks a real email address.

Co-Authored-By: Vico <[email protected]>
Co-Authored-By: Josh Kelley <[email protected]>
@chbrown

chbrown commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Haha I was just working (/ having my Claude work) on this, this morning, and came up with basically the same final step (atop others) as your change here. Sorry! Great (Claude Opus 5.5) minds think alike, I guess.

4cedb42 obsoletes this, and builds on other improvements that make diffArrays dramatically more memory efficient, ~100x faster on smallish edits, and ~10000x faster on single edits in large arrays.

@chbrown

chbrown commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Anyway, thanks, closing this, but feel free to open another PR with further improvements like whatever the docmost repo is doing.

There's now a benchmark directory in master that'd be useful to compare with.

@chbrown chbrown closed this Nov 21, 2026
@vicocotea

Copy link
Copy Markdown
Contributor Author

I’ll leave you to continue working on the development. There’s some optimization to be gained by using the Ukkonen/Myers algorithm for the diff, similar to how it’s done in Docmost.

Happy coding!

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.

2 participants