Add benchmark for holey-array key enumeration cost#2508
Merged
Conversation
Object.keys/values/entries on an array scale with the array's logical length rather than the number of populated elements. Ordinary 'a[index] = value' assignment keeps the backing store dense for any index below the 10M dense-array threshold: SetIndexValue bumps the length to index+1 via EnsureCorrectLength before the 'looks sparse' heuristic in WriteArrayValueUnlikely runs, so that heuristic (index < denseHeadroom + 50) never fires for plain assignment. A holey array therefore keeps a backing store proportional to its max index, and GetOwnPropertyKeys allocates a List of that size and scans every slot to produce only the populated keys. The benchmark holds the populated count fixed at 500 and varies only the gap between indices; time and allocations grow ~linearly with the gap (47x slower / 457x more allocated from gap 1 to gap 1000). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lahma
force-pushed
the
bench-holey-array-enumeration
branch
from
June 8, 2026 17:50
63ca24a to
bed385a
Compare
lahma
enabled auto-merge (squash)
June 8, 2026 17:53
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.
Summary
Benchmark showing array key enumeration scales with logical length, not element count.
Details
While benchmarking #2501 I hit a surprisingly large allocation enumerating a holey array, and tracked it to a general property worth recording:
Object.keys/values/entries(and any[[OwnPropertyKeys]]consumer) on an array scale with the array's logical length rather than the number of populated elements.Root cause: ordinary
a[index] = valueassignment keeps the backing store dense for any index below the 10M dense-array threshold.SetIndexValuecallsEnsureCorrectLength(index), which bumps the length toindex + 1beforeWriteArrayValueUnlikelyruns its "looks sparse" check:Because
declaredLengthwas just set toindex + 1, the guard is effectivelyindex < (index + 1) + 50, which is always true — so plain assignment never converts to sparse. (Sparse mode is only reached vianew Array(n)withn >= 10M, orObject.definePropertyon an index, which goes throughConvertToSparse.)A holey array therefore keeps a backing store proportional to its max index, and
GetOwnPropertyKeysboth allocates aList<JsValue>of that size and scans every slot, even though only the populated indices are returned.The benchmark holds the populated count fixed at 500 and varies only the gap between indices (so the logical length is
500 * Gap):AMD Ryzen 9 5950X,.NET 10.0.8,--job short. Same number of keys throughout, yet the gap-1000 case is ~47× slower and allocates ~457× more than the packed case.This PR only adds the benchmark to document and quantify the behavior — there is no functional change. An actual fix would mean reworking the dense/sparse heuristic so plain assignment can fall back to sparse for genuinely holey arrays (distinguishing a holey scatter from a legitimate lazy
new Array(N)capacity fill), which is a broader change best validated separately against Test262.Linked issue
Refs #2501
Test plan
Jint.Testsdotnet run -c Release --project Jint.Benchmark -- --filter *ArrayHoleyKeysBenchmark*Jint.Tests.Test262Breaking change?
No — benchmark only, no runtime change.