Question presented to candidate: "V8 has a well-known copy-on-write optimization for JavaScript arrays. Do the new ES2023 methods toSorted, toReversed, toSpliced, and with take advantage of that COW sharing to make their copy lazy, or do they always eagerly copy? Show me, do not just tell me."
What a strong answer should cover:
- V8 genuinely has a copy-on-write (COW) backing-store optimization for arrays: when an array's element storage is marked COW (most commonly, a freshly-evaluated array literal), a shallow clone via slice() or spread [...arr] can share the exact same backing FixedArray instead of allocating a new one — verified directly by inspecting V8 internals.
- The four ES2023 change-array-by-copy methods do NOT participate in that lazy sharing. Each one allocates a brand-new, non-COW backing store immediately at call time, verified directly — even with(index, value), which only changes a single element, does not share the other unchanged elements.
- This makes sense once the two questions are separated: COW sharing is about WHEN a copy physically happens for methods that produce an array that starts out identical to the source. toSorted/toReversed/toSpliced/with all produce arrays that are already structurally different the moment they return, so there is nothing identical left to lazily share.
- These four methods should be budgeted as a genuine O(n) allocation on every call, confirmed by direct benchmark: with() on a 20,000-element array costs essentially the same as a manual spread-then-assign copy.
- Contrast with slice()/spread, which really can be near-free when the source is COW-eligible, verified via a real backing-store-address comparison before and after the call.
Clarifying questions expected:
- "Are we talking about V8 specifically, or does the ECMAScript spec itself mandate a particular allocation strategy?" — the spec only mandates the observable result (an independent array); COW sharing is a pure V8 implementation detail other engines are free to implement differently.
- "Does the source array's own origin matter — a literal versus something built with Array.from or map?" — worth naming that COW eligibility in V8 is tied to how the source array itself was created, not a universal property of every array.
Code / implementation expected: Yes — direct inspection of V8's internal backing-store pointers via --allow-natives-syntax and %DebugPrint, not a description from memory, plus a real benchmark comparing native with() against a manual copy.