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); } /**