Files
node/benchmark/webstreams/readable-async-iterator.js
Matteo Collina f91697a988 benchmark: add bytes variant to webstreams async-iterator
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/64291
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-07-07 09:38:25 +00:00

49 lines
920 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 }) {
const rs = type === 'bytes' ?
new ReadableStream({
type: 'bytes',
pull: function(controller) {
controller.enqueue(new Uint8Array(1));
},
}) :
new ReadableStream({
pull: function(controller) {
controller.enqueue(1);
},
});
let x = 0;
bench.start();
if (type === 'bytes') {
for await (const chunk of rs) {
x += chunk.byteLength;
if (x > n) {
break;
}
}
} else {
for await (const chunk of rs) {
x += chunk;
if (x > n) {
break;
}
}
}
// Use x to ensure V8 does not optimize away the loop as a noop.
console.assert(x);
bench.end(n);
}