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 <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66504
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
James M Snell authored and Antoine du Hamel committed 2026-11-25 18:00:50 +01:00
1 parent f177d0f276
commit 3680b2496e
6 files changed
+43 -50

No files matched your search

+3 -3
View File
@@ -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);
+4 -5
View File
@@ -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 = [];
+19 -19
View File
@@ -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;
+1 -1
View File
@@ -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();
+10 -15
View File
@@ -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;
+6 -7
View File
@@ -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;
}