Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: add a benchmark for read() of ReadableStreams #49622

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions benchmark/webstreams/readable-read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
const common = require('../common.js');
const { ReadableStream } = require('node:stream/web');

const bench = common.createBenchmark(main, {
n: [1e5],
type: ['normal', 'byob'],
});

async function main({ n, type }) {
switch (type) {
case 'normal': {
const rs = new ReadableStream({
pull: function(controller) {
controller.enqueue('a');
},
});
const reader = rs.getReader();
let x = null;
bench.start();
for (let i = 0; i < n; i++) {
const { value } = await reader.read();
x = value;
}
bench.end(n);
console.assert(x);
break;
}
case 'byob': {
const encode = new TextEncoder();
const rs = new ReadableStream({
type: 'bytes',
pull: function(controller) {
controller.enqueue(encode.encode('a'));
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
},
});
const reader = rs.getReader({ mode: 'byob' });
let x = null;
bench.start();
for (let i = 0; i < n; i++) {
const { value } = await reader.read(new Uint8Array(1));
debadree25 marked this conversation as resolved.
Show resolved Hide resolved
x = value;
}
bench.end(n);
console.assert(x);
break;
}
}
}