Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: ab1caf11703db99e13e31aae7406a3aa68909f9b
https://github.com/WebKit/WebKit/commit/ab1caf11703db99e13e31aae7406a3aa68909f9b
Author: Sosuke Suzuki <[email protected]>
Date: 2026-09-16 (Wed, 16 Sep 2026)
Changed paths:
A JSTests/microbenchmarks/array-length-increment-large.js
A JSTests/stress/array-set-length-grow-within-vector.js
M Source/JavaScriptCore/runtime/JSArray.cpp
Log Message:
-----------
[JSC] `JSArray::setLength` should not count elements when the new length fits
in the vector
https://bugs.webkit.org/show_bug.cgi?id=324306
Reviewed by Yusuke Suzuki.
For Int32 / Double / Contiguous arrays, JSArray::setLength calls
countElements() whenever newLength >= MIN_SPARSE_ARRAY_INDEX (100000) to
decide whether to switch to ArrayStorage. countElements() is O(length), so
growing an array with `++array.length` is quadratic past 100000 elements:
200000 iterations take 1.6 s, while 99999 take 2 ms [1].
Skip the density check when newLength fits in the current vectorLength.
The storage is already allocated, so switching to a sparse map saves
nothing. ensureLength() grows the vector geometrically, so the check now
runs O(log n) times for such a loop.
Baseline Patched
array-length-increment-large 2123.6537+-19.7697 ^ 5.3079+-0.2808
^ definitely 400.0913x faster
array-length-reset 14.0863+-0.5243 13.8476+-0.5210
might be 1.0172x faster
array-length-reset-double 14.4644+-0.4715 ? 14.6520+-0.6321
? might be 1.0130x slower
[1]: https://github.com/oven-sh/bun/issues/25559
Tests: JSTests/microbenchmarks/array-length-increment-large.js
JSTests/stress/array-set-length-grow-within-vector.js
* JSTests/microbenchmarks/array-length-increment-large.js: Added.
(test):
* JSTests/stress/array-set-length-grow-within-vector.js: Added.
(shouldBe):
(incrementLength):
* Source/JavaScriptCore/runtime/JSArray.cpp:
(JSC::JSArray::setLength):
Canonical link: https://commits.webkit.org/321297@main
To unsubscribe from these emails, change your notification settings at
https://github.com/WebKit/WebKit/settings/notifications