From ba74ca5c5bda0eb575424e661336d7077462e1fe Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 7 Oct 2026 12:47:22 +0000 Subject: [PATCH] stream: create stream/iter from() value streams as class instances from() of a value that needs no normalization (a string, a buffer, a Uint8Array[]) returned an object literal with a null prototype, which V8 creates in dictionary mode, and each of its iterators was an object literal passed to ObjectSetPrototypeOf(), a runtime call. Together they were three quarters of the time to create and read such a stream. Use two classes, BatchSource and BatchIterator, whose prototypes do not inherit from Object.prototype either, as before. Creating a stream from a 16-byte chunk and reading it is about 6.5 times faster, and from a one-chunk array about 5.5 times faster. 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/from.js | 110 ++++++++++++++++++------------ 1 file changed, 65 insertions(+), 45 deletions(-) diff --git a/lib/internal/streams/iter/from.js b/lib/internal/streams/iter/from.js index d32d2353bdc..0c962b4a8f9 100644 --- a/lib/internal/streams/iter/from.js +++ b/lib/internal/streams/iter/from.js @@ -1651,6 +1651,70 @@ function fromSync(input) { }; } +// BatchSource and BatchIterator are classes, rather than object literals +// with a null prototype, which V8 creates in dictionary mode: from() of a +// value is often called once per stream. Their prototypes do not inherit +// from Object.prototype either. +class BatchIterator { + #batch; + #index = 0; + + constructor(batch) { + this.#batch = batch; + } + + // The next batch, or null once done. + #nextBatch() { + const batch = this.#batch; + const index = this.#index; + if (index >= batch.length) { + this.#index = batch.length; + return null; + } + if (index === 0 && batch.length <= FROM_BATCH_SIZE) { + this.#index = batch.length; + return batch; + } + this.#index = index + FROM_BATCH_SIZE; + return ArrayPrototypeSlice(batch, index, index + FROM_BATCH_SIZE); + } + + next() { + const value = this.#nextBatch(); + return PromiseResolve(value === null ? + new IterResult(true, undefined) : new IterResult(false, value)); + } + + return(value) { + this.#index = this.#batch.length; + return PromiseResolve(new IterResult(true, value)); + } + + throw(error) { + this.#index = this.#batch.length; + return PromiseReject(error); + } + + [SymbolAsyncIterator]() { + return this; + } +} +ObjectSetPrototypeOf(BatchIterator.prototype, null); + +class BatchSource { + #batch; + + constructor(batch) { + this.#batch = batch; + } + + [SymbolAsyncIterator]() { + return new BatchIterator(this.#batch); + } +} +BatchSource.prototype[kValidatedSource] = true; +ObjectSetPrototypeOf(BatchSource.prototype, null); + /** * An async iterable yielding the chunks of `batch`, a Uint8Array[], in * batches of at most FROM_BATCH_SIZE chunks, for from() of values that need @@ -1662,51 +1726,7 @@ function fromSync(input) { * @returns {AsyncIterable} */ function createBatchSource(batch) { - return { - __proto__: null, - [SymbolAsyncIterator]() { - return createBatchIterator(batch); - }, - [kValidatedSource]: true, - }; -} - -function createBatchIterator(batch) { - let index = 0; - - // The next batch, or null once done. - function nextBatch() { - if (index >= batch.length) { - index = batch.length; - return null; - } - if (index === 0 && batch.length <= FROM_BATCH_SIZE) { - index = batch.length; - return batch; - } - const start = index; - index += FROM_BATCH_SIZE; - return ArrayPrototypeSlice(batch, start, index); - } - - return ObjectSetPrototypeOf({ - next() { - const value = nextBatch(); - return PromiseResolve(value === null ? - new IterResult(true, undefined) : new IterResult(false, value)); - }, - return(value) { - index = batch.length; - return PromiseResolve(new IterResult(true, value)); - }, - throw(error) { - index = batch.length; - return PromiseReject(error); - }, - [SymbolAsyncIterator]() { - return this; - }, - }, null); + return new BatchSource(batch); } /**