Commit cdb4bb33192 for nodejs
commit cdb4bb33192f220058bd337e6d1046c9dd0da5e1
Author: James M Snell <jasnell@gmail.com>
Date: Wed Oct 7 12:47:22 2026 +0000
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 <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>
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,62 +1651,82 @@ function fromSync(input) {
};
}
-/**
- * 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
- * no normalization. Each iterator does what an async generator yielding the
- * batches would, without the generator object and the promises and frames
- * of resuming it: next() gives the batches, then done; return() and throw()
- * end the iteration, throw() rejecting with its argument.
- * @param {Uint8Array[]} batch
- * @returns {AsyncIterable<Uint8Array[]>}
- */
-function createBatchSource(batch) {
- return {
- __proto__: null,
- [SymbolAsyncIterator]() {
- return createBatchIterator(batch);
- },
- [kValidatedSource]: true,
- };
-}
+// 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;
-function createBatchIterator(batch) {
- let index = 0;
+ constructor(batch) {
+ this.#batch = batch;
+ }
// The next batch, or null once done.
- function nextBatch() {
+ #nextBatch() {
+ const batch = this.#batch;
+ const index = this.#index;
if (index >= batch.length) {
- index = batch.length;
+ this.#index = batch.length;
return null;
}
if (index === 0 && batch.length <= FROM_BATCH_SIZE) {
- index = batch.length;
+ this.#index = batch.length;
return batch;
}
- const start = index;
- index += FROM_BATCH_SIZE;
- return ArrayPrototypeSlice(batch, start, index);
+ this.#index = index + FROM_BATCH_SIZE;
+ return ArrayPrototypeSlice(batch, index, index + FROM_BATCH_SIZE);
}
- 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);
+ 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
+ * no normalization. Each iterator does what an async generator yielding the
+ * batches would, without the generator object and the promises and frames
+ * of resuming it: next() gives the batches, then done; return() and throw()
+ * end the iteration, throw() rejecting with its argument.
+ * @param {Uint8Array[]} batch
+ * @returns {AsyncIterable<Uint8Array[]>}
+ */
+function createBatchSource(batch) {
+ return new BatchSource(batch);
}
/**