fix(core): stable zIndex child sort for Chrome < 70#101
Merged
Conversation
Array.prototype.sort is unstable on Chrome < 70 (V8 quicksorts arrays longer than 10 elements), so equal-zIndex siblings could silently reshuffle paint order on embedded targets. Replace the native sort in sortChildren with a stable, allocation-free insertion sort — children arrays are small and nearly sorted, so it is O(n) in practice. Also removes the unused bucketSortByZIndex, which indexed buckets by zIndex - min: legal huge zIndex values created enormous sparse arrays, and below-min or fractional values leaked as object properties. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
What changed
CoreNode.sortChildren()now uses a stable, allocation-free insertion sort (sortByZIndexStableinsrc/core/lib/collectionUtils.ts) instead ofArray.prototype.sort.bucketSortByZIndexand the now-unneeded hoistedcompareZIndexcomparator.sortByZIndexStable(src/core/lib/collectionUtils.test.ts).Why
Array.prototype.sortis not stable on Chrome < 70 — V8 used an unstable quicksort for arrays longer than 10 elements. On our Chrome 38 embedded targets, equal-zIndex siblings (the common case: most children have zIndex 0) could silently reshuffle paint order whenever a parent with more than 10 children re-sorted. Insertion sort is stable, zero-allocation (no comparator closure, no buckets), and O(n) on the nearly-sorted children arrays this path actually sees — a re-sort typically follows a single zIndex change.The deleted
bucketSortByZIndexwas dead code (no imports) with real defects: it indexed buckets byzIndex - min, so a legal huge zIndex (the setter only clamps beyondMAX_SAFE_INTEGER) allocated an enormous sparse array, and below-minor fractional values landed in negative/fractional indices stored as object properties thatbuckets.length = 0never cleaned up.Notes for reviewers
🤖 Generated with Claude Code