Amemiya Riya
unread,Jan 28, 2026, 4:33:32 AM (yesterday) Jan 28Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to v8-dev
Hi V8 team,
I’m working on a patch to add a C++ fast path for Array.prototype.flat
(2-pass: precompute output length, preallocate, then write directly).
This is modeled after JSC’s C++ implementation but adapted for V8’s
elements/protector model.
Summary of changes:
- Add runtime fast path for Array.prototype.flat
- Preconditions: FastJSArray + NoElements/ArraySpecies protectors intact
- Proxy/accessor/custom elements bail out to existing slow path
- Recursive flatten only when elements are JSArray; otherwise bail
- If length exceeds 2^53-1, throw TypeError (same as spec)
Why:
- Current implementation writes element-by-element via property creation.
- For large arrays / deep nesting, we can significantly reduce overhead.
Correctness:
- holes are skipped (no property), consistent with spec
- fallback is always used when protos/Proxy/accessors exist
Performance (single-run huge array):
- input: outer=20000, chunk=1024 (output length 20,480,000)
- d8 fast (with patch): 58ms
- d8 slow (NoElements invalidated): 924ms
- speedup: ~16x
Tests run:
- mjsunit/harmony/array-flat
- mjsunit/harmony/array-flatMap
- mjsunit/harmony/array-flat-species
- mjsunit/harmony/array-flatMap-species
- (additional large test run completed)
I can share the patch and more detailed numbers if helpful.
Is this direction acceptable, or would you recommend any constraints or
additional checks?
Thanks!