Files
node/benchmark/webstreams/tee.js
T
Matteo Collina c543cfb72b stream: use the ring buffer for pending BYOB pull-into descriptors
The byte controller's [[pendingPullIntos]] list was still a plain array
consumed with ArrayPrototypeShift, while every other per-chunk queue in
the WHATWG streams implementation has moved to the Queue ring buffer.
BYOB reads push and shift one descriptor per read, and Array.prototype
shift has real per-call cost even at length 1.

Back the descriptor list with the same lazily materialized Queue used
for the request queues, so constructing a byte stream still allocates
no descriptor storage.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/64818
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
2026-07-31 22:33:39 +00:00

45 lines
999 B
JavaScript

'use strict';
const common = require('../common.js');
const { ReadableStream } = require('node:stream/web');
const bench = common.createBenchmark(main, {
n: [1e5],
type: ['normal', 'bytes'],
});
async function main({ n, type }) {
let i = 0;
const source = type === 'bytes' ?
{
type: 'bytes',
pull(controller) {
if (i++ < n) controller.enqueue(new Uint8Array(16));
else controller.close();
},
} :
{
pull(controller) {
if (i++ < n) controller.enqueue('a');
else controller.close();
},
};
const rs = new ReadableStream(source);
const [branch1, branch2] = rs.tee();
const reader1 = branch1.getReader();
const reader2 = branch2.getReader();
let reads = 0;
bench.start();
for (;;) {
const [result1, result2] = await Promise.all([
reader1.read(),
reader2.read(),
]);
if (result1.done || result2.done) break;
reads++;
}
bench.end(reads);
console.assert(reads === n);
}