Skip to content

Commit 8a89642

Browse files
rluvatonUlisesGascon
authored andcommitted
benchmark: add more cases to Readable.from
PR-URL: #50351 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent dbe6c5f commit 8a89642

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

benchmark/streams/readable-from.js

+48-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,58 @@ const Readable = require('stream').Readable;
55

66
const bench = common.createBenchmark(main, {
77
n: [1e7],
8+
type: ['array', 'sync-generator-with-sync-values', 'sync-generator-with-async-values', 'async-generator'],
89
});
910

10-
async function main({ n }) {
11-
const arr = [];
12-
for (let i = 0; i < n; i++) {
13-
arr.push(`${i}`);
11+
async function main({ n, type }) {
12+
let fromArg;
13+
14+
switch (type) {
15+
case 'array': {
16+
fromArg = [];
17+
for (let i = 0; i < n; i++) {
18+
fromArg.push(`${i}`);
19+
}
20+
21+
break;
22+
}
23+
24+
case 'sync-generator-with-sync-values': {
25+
fromArg = (function* () {
26+
for (let i = 0; i < n; i++) {
27+
yield `${i}`;
28+
}
29+
})();
30+
31+
break;
32+
}
33+
34+
case 'sync-generator-with-async-values': {
35+
fromArg = (function* () {
36+
for (let i = 0; i < n; i++) {
37+
yield Promise.resolve(`${i}`);
38+
}
39+
})();
40+
41+
break;
42+
}
43+
44+
case 'async-generator': {
45+
fromArg = (async function* () {
46+
for (let i = 0; i < n; i++) {
47+
yield `${i}`;
48+
}
49+
})();
50+
51+
break;
52+
}
53+
54+
default: {
55+
throw new Error(`Unknown type: ${type}`);
56+
}
1457
}
1558

16-
const s = new Readable.from(arr);
59+
const s = new Readable.from(fromArg);
1760

1861
bench.start();
1962
s.on('data', (data) => {

0 commit comments

Comments
 (0)