From 3680b2496ebefee7e88bf15074fe558ae97dfe91 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 4 Oct 2026 06:09:36 +0000 Subject: [PATCH] stream: append to arrays with indexed stores in stream/iter hot paths ArrayPrototypePush() is listed among the primordials with known performance issues. Append with an indexed store instead where stream/iter collects chunks or batches: batching sources in from() and fromSync(), flattening transform output in pull() and pullSync(), the batches of push() and of Readable sources, the output of the zlib/iter transforms and the chunks collected by bytes() and similar consumers. Like ArrayPrototypePush(), an indexed store is unaffected by changes to Array.prototype.push and runs setters defined for indices on Array.prototype. bytes() is about 13% faster, pullSync() through a generator transform and push() about 6%, and the other paths up to 4%. Assisted-by: OpenCode Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/66504 Reviewed-By: Matteo Collina Reviewed-By: Trivikram Kamat --- lib/internal/streams/iter/classic.js | 6 ++-- lib/internal/streams/iter/from.js | 9 +++--- lib/internal/streams/iter/pull.js | 38 +++++++++++++------------- lib/internal/streams/iter/push.js | 2 +- lib/internal/streams/iter/transform.js | 25 +++++++---------- lib/internal/streams/iter/utils.js | 13 ++++----- 6 files changed, 43 insertions(+), 50 deletions(-) diff --git a/lib/internal/streams/iter/classic.js b/lib/internal/streams/iter/classic.js index c1ed8ad51f5..2b39bc92787 100644 --- a/lib/internal/streams/iter/classic.js +++ b/lib/internal/streams/iter/classic.js @@ -160,14 +160,14 @@ async function normalizeBatch(raw) { for (let i = 0; i < raw.length; i++) { const value = raw[i]; if (isUint8Array(value)) { - ArrayPrototypePush(batch, value); + batch[batch.length] = value; } else { // normalizeAsyncValue may await for async protocols (e.g. // toAsyncStreamable on yielded objects). Stream events during // the suspension are queued, not lost -- errors will surface // on the next loop iteration after this yield completes. for await (const normalized of normalizeAsyncValue(value)) { - ArrayPrototypePush(batch, normalized); + batch[batch.length] = normalized; } } } @@ -229,7 +229,7 @@ function createBatchedAsyncIterator(stream, normalize) { stream._readableState?.length > 0) { const c = stream.read(); if (c === null) break; - ArrayPrototypePush(batch, c); + batch[batch.length] = c; } if (normalize !== null) { const result = await normalize(batch); diff --git a/lib/internal/streams/iter/from.js b/lib/internal/streams/iter/from.js index 47b71e09a49..c249b0e86f1 100644 --- a/lib/internal/streams/iter/from.js +++ b/lib/internal/streams/iter/from.js @@ -9,7 +9,6 @@ const { ArrayBufferIsView, ArrayIsArray, ArrayPrototypeEvery, - ArrayPrototypePush, ArrayPrototypeSlice, DataViewPrototypeGetBuffer, DataViewPrototypeGetByteLength, @@ -347,7 +346,7 @@ function* normalizeSyncSource(source) { } // Fast path 2: value is a single Uint8Array (very common) if (isUint8Array(value)) { - ArrayPrototypePush(batch, value); + batch[batch.length] = value; if (batch.length === FROM_BATCH_SIZE) { yield batch; batch = []; @@ -361,7 +360,7 @@ function* normalizeSyncSource(source) { } let valueBatch = []; for (const chunk of normalizeSyncValue(value)) { - ArrayPrototypePush(valueBatch, chunk); + valueBatch[valueBatch.length] = chunk; if (valueBatch.length === FROM_BATCH_SIZE) { yield valueBatch; valueBatch = []; @@ -658,7 +657,7 @@ async function* normalizeAsyncSourceValue(value, context, } continue; } - ArrayPrototypePush(batch, chunk); + batch[batch.length] = chunk; if (batch.length === FROM_BATCH_SIZE) { yield batch; batch = []; @@ -898,7 +897,7 @@ function* readSyncSource(source, context) { } // Fast path 2: value is a single Uint8Array (very common) if (isUint8Array(value)) { - ArrayPrototypePush(batch, value); + batch[batch.length] = value; if (batch.length === FROM_BATCH_SIZE) { yield batch; batch = []; diff --git a/lib/internal/streams/iter/pull.js b/lib/internal/streams/iter/pull.js index b59949ff1f3..7cc7f1169c4 100644 --- a/lib/internal/streams/iter/pull.js +++ b/lib/internal/streams/iter/pull.js @@ -264,7 +264,7 @@ function* processTransformResultSync(result) { const batch = []; for (const item of result) { for (const chunk of flattenTransformYieldSync(item)) { - ArrayPrototypePush(batch, chunk); + batch[batch.length] = chunk; } } if (batch.length > 0) { @@ -290,28 +290,28 @@ function appendTransformResultSync(target, result) { } if (isUint8ArrayBatch(result)) { if (result.length > 0) { - ArrayPrototypePush(target, result); + target[target.length] = result; } return; } if (isUint8Array(result)) { - ArrayPrototypePush(target, [result]); + target[target.length] = [result]; return; } if (typeof result === 'string') { - ArrayPrototypePush(target, [toUint8Array(result)]); + target[target.length] = [toUint8Array(result)]; return; } if (isAnyArrayBuffer(result)) { - ArrayPrototypePush(target, [new Uint8Array(result)]); + target[target.length] = [new Uint8Array(result)]; return; } if (ArrayBufferIsView(result)) { - ArrayPrototypePush(target, [arrayBufferViewToUint8Array(result)]); + target[target.length] = [arrayBufferViewToUint8Array(result)]; return; } for (const batch of processTransformResultSync(result)) { - ArrayPrototypePush(target, batch); + target[target.length] = batch; } } @@ -360,11 +360,11 @@ async function* processTransformResultAsync(result) { const batch = []; for await (const item of result) { if (isUint8Array(item)) { - ArrayPrototypePush(batch, item); + batch[batch.length] = item; continue; } for await (const chunk of flattenTransformYieldAsync(item)) { - ArrayPrototypePush(batch, chunk); + batch[batch.length] = chunk; } } if (batch.length > 0) { @@ -377,13 +377,13 @@ async function* processTransformResultAsync(result) { const batch = []; for (const item of result) { if (isUint8Array(item)) { - ArrayPrototypePush(batch, item); + batch[batch.length] = item; continue; } // Note: This iteration is synchronous, since async iterables // may not be nested within sync iterables. for (const chunk of flattenTransformYieldSync(item)) { - ArrayPrototypePush(batch, chunk); + batch[batch.length] = chunk; } } if (batch.length > 0) { @@ -410,24 +410,24 @@ function appendTransformResultAsync(target, result) { } if (isUint8ArrayBatch(result)) { if (result.length > 0) { - ArrayPrototypePush(target, result); + target[target.length] = result; } return; } if (isUint8Array(result)) { - ArrayPrototypePush(target, [result]); + target[target.length] = [result]; return; } if (typeof result === 'string') { - ArrayPrototypePush(target, [toUint8Array(result)]); + target[target.length] = [toUint8Array(result)]; return; } if (isAnyArrayBuffer(result)) { - ArrayPrototypePush(target, [new Uint8Array(result)]); + target[target.length] = [new Uint8Array(result)]; return; } if (ArrayBufferIsView(result)) { - ArrayPrototypePush(target, [arrayBufferViewToUint8Array(result)]); + target[target.length] = [arrayBufferViewToUint8Array(result)]; return; } return appendTransformResultAsyncSlow(target, result); @@ -435,7 +435,7 @@ function appendTransformResultAsync(target, result) { async function appendTransformResultAsyncSlow(target, result) { for await (const batch of processTransformResultAsync(result)) { - ArrayPrototypePush(target, batch); + target[target.length] = batch; } } @@ -533,7 +533,7 @@ function* applyStatefulSyncTransform(source, transform, receiver) { if (item === null) continue; const batch = []; for (const chunk of flattenTransformYieldSync(item)) { - ArrayPrototypePush(batch, chunk); + batch[batch.length] = chunk; } if (batch.length > 0) { yield batch; @@ -968,7 +968,7 @@ async function* applyStatefulAsyncTransform( // Slow path: flatten arbitrary transform yield const batch = []; for await (const chunk of flattenTransformYieldAsync(item)) { - ArrayPrototypePush(batch, chunk); + batch[batch.length] = chunk; } if (batch.length > 0) { yield batch; diff --git a/lib/internal/streams/iter/push.js b/lib/internal/streams/iter/push.js index c99253af43e..8b6eb2da536 100644 --- a/lib/internal/streams/iter/push.js +++ b/lib/internal/streams/iter/push.js @@ -508,7 +508,7 @@ class PushQueue { for (let i = 0; i < this.#slots.length; i++) { const batch = validateBatchEntry(this.#slots.get(i)); for (let j = 0; j < batch.length; j++) { - ArrayPrototypePush(result, batch[j]); + result[result.length] = batch[j]; } } this.#slots.clear(); diff --git a/lib/internal/streams/iter/transform.js b/lib/internal/streams/iter/transform.js index 8e60589aba3..e7239278500 100644 --- a/lib/internal/streams/iter/transform.js +++ b/lib/internal/streams/iter/transform.js @@ -10,7 +10,6 @@ const { ArrayPrototypeMap, - ArrayPrototypePush, ArrayPrototypeSlice, FunctionPrototypeCall, MathMax, @@ -333,18 +332,16 @@ function makeZlibTransform(createHandleFn, processFlag, finishFlag) { if (have > 0) { if (bufferExhausted && outOffset === 0) { // Entire buffer filled from start - yield directly, no copy. - ArrayPrototypePush(pending, outBuf); + pending[pending.length] = outBuf; } else if (bufferExhausted) { // Tail of buffer filled and buffer is being replaced - // subarray is safe since outBuf reference is overwritten below. - ArrayPrototypePush(pending, - outBuf.subarray(outOffset, outOffset + have)); + pending[pending.length] = + outBuf.subarray(outOffset, outOffset + have); } else { // Partial fill, buffer will be reused - must copy. - ArrayPrototypePush(pending, - TypedArrayPrototypeSlice(outBuf, - outOffset, - outOffset + have)); + pending[pending.length] = + TypedArrayPrototypeSlice(outBuf, outOffset, outOffset + have); } pendingBytes += have; outOffset += have; @@ -642,17 +639,15 @@ function makeZlibTransformSync(createHandleFn, processFlag, finishFlag) { if (have > 0) { if (bufferExhausted && outOffset === 0) { // Entire buffer filled - yield directly, no copy. - ArrayPrototypePush(pending, outBuf); + pending[pending.length] = outBuf; } else if (bufferExhausted) { // Tail filled, buffer being replaced - subarray is safe. - ArrayPrototypePush(pending, - outBuf.subarray(outOffset, outOffset + have)); + pending[pending.length] = + outBuf.subarray(outOffset, outOffset + have); } else { // Partial fill, buffer reused - must copy. - ArrayPrototypePush(pending, - TypedArrayPrototypeSlice(outBuf, - outOffset, - outOffset + have)); + pending[pending.length] = + TypedArrayPrototypeSlice(outBuf, outOffset, outOffset + have); } pendingBytes += have; outOffset += have; diff --git a/lib/internal/streams/iter/utils.js b/lib/internal/streams/iter/utils.js index 2d85d888ddf..82dace3d458 100644 --- a/lib/internal/streams/iter/utils.js +++ b/lib/internal/streams/iter/utils.js @@ -5,7 +5,6 @@ const { ArrayBufferPrototypeGetByteLength, ArrayBufferPrototypeGetDetached, ArrayBufferPrototypeGetResizable, - ArrayPrototypePush, ArrayPrototypeSlice, FunctionPrototypeCall, ObjectDefineProperty, @@ -555,12 +554,12 @@ function validateBudget(budget) { function recordChunk(chunks, checks, value) { const buffer = TypedArrayPrototypeGetBuffer(value); const byteLength = TypedArrayPrototypeGetByteLength(value); - ArrayPrototypePush(chunks, value); + chunks[chunks.length] = value; if (byteLength === 0 || isSharedArrayBuffer(buffer) || ArrayBufferPrototypeGetResizable(buffer)) { - ArrayPrototypePush(checks, snapshotByteView(value)); + checks[checks.length] = snapshotByteView(value); } else { - ArrayPrototypePush(checks, byteLength); + checks[checks.length] = byteLength; } return byteLength; } @@ -612,14 +611,14 @@ function splitBatchEntry(entry, limit) { for (let i = 0; i < views.length; i++) { const view = views[i]; if (current.length > 0 && byteLength + view.byteLength >= limit) { - ArrayPrototypePush(entries, new BatchEntry(current, byteLength)); + entries[entries.length] = new BatchEntry(current, byteLength); current = []; byteLength = 0; } - ArrayPrototypePush(current, view); + current[current.length] = view; byteLength += view.byteLength; } - ArrayPrototypePush(entries, new BatchEntry(current, byteLength)); + entries[entries.length] = new BatchEntry(current, byteLength); return entries; }