benchmark: add more cases to Readable.from

PR-URL: https://github.com/nodejs/node/pull/50351
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
This commit is contained in:
Raz Luvaton 2023-10-24 04:36:17 +03:00 committed by GitHub
parent abd8ff6f78
commit 0fb512344f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,15 +5,58 @@ const Readable = require('stream').Readable;
const bench = common.createBenchmark(main, {
n: [1e7],
type: ['array', 'sync-generator-with-sync-values', 'sync-generator-with-async-values', 'async-generator'],
});
async function main({ n }) {
const arr = [];
for (let i = 0; i < n; i++) {
arr.push(`${i}`);
async function main({ n, type }) {
let fromArg;
switch (type) {
case 'array': {
fromArg = [];
for (let i = 0; i < n; i++) {
fromArg.push(`${i}`);
}
break;
}
case 'sync-generator-with-sync-values': {
fromArg = (function* () {
for (let i = 0; i < n; i++) {
yield `${i}`;
}
})();
break;
}
case 'sync-generator-with-async-values': {
fromArg = (function* () {
for (let i = 0; i < n; i++) {
yield Promise.resolve(`${i}`);
}
})();
break;
}
case 'async-generator': {
fromArg = (async function* () {
for (let i = 0; i < n; i++) {
yield `${i}`;
}
})();
break;
}
default: {
throw new Error(`Unknown type: ${type}`);
}
}
const s = new Readable.from(arr);
const s = new Readable.from(fromArg);
bench.start();
s.on('data', (data) => {