benchmark: include webstreams benchmark · nodejs/node@cf2ff81 · GitHub
Skip to content

Commit cf2ff81

Browse files
committed
benchmark: include webstreams benchmark
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #45876 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent 9ed547b commit cf2ff81

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

benchmark/webstreams/creation.js

Lines changed: 49 additions & 0 deletions

benchmark/webstreams/pipe-to.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const {
4+
ReadableStream,
5+
WritableStream,
6+
} = require('node:stream/web');
7+
8+
const bench = common.createBenchmark(main, {
9+
n: [5e6],
10+
highWaterMarkR: [512, 1024, 2048, 4096],
11+
highWaterMarkW: [512, 1024, 2048, 4096],
12+
});
13+
14+
15+
async function main({ n, highWaterMarkR, highWaterMarkW }) {
16+
const b = Buffer.alloc(1024);
17+
let i = 0;
18+
const rs = new ReadableStream({
19+
highWaterMark: highWaterMarkR,
20+
pull: function(controller) {
21+
if (i++ === n) {
22+
controller.enqueue(b);
23+
} else {
24+
controller.close();
25+
}
26+
}
27+
});
28+
const ws = new WritableStream({
29+
highWaterMark: highWaterMarkW,
30+
write(chunk, controller) {},
31+
close() { bench.end(n); },
32+
});
33+
34+
bench.start();
35+
rs.pipeTo(ws);
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const {
4+
ReadableStream,
5+
} = require('node:stream/web');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e5],
9+
});
10+
11+
12+
async function main({ n }) {
13+
const rs = new ReadableStream({
14+
pull: function(controller) {
15+
controller.enqueue(1);
16+
}
17+
});
18+
19+
let x = 0;
20+
21+
bench.start();
22+
for await (const chunk of rs) {
23+
x += chunk;
24+
if (x > n) {
25+
break;
26+
}
27+
}
28+
// Use x to ensure V8 does not optimize away the loop as a noop.
29+
console.assert(x);
30+
bench.end(n);
31+
}
Lines changed: 7 additions & 0 deletions

0 commit comments

Comments
 (0)