Skip to content

Commit

Permalink
benchmark: add more cases to Readable.from
Browse files Browse the repository at this point in the history
PR-URL: nodejs#50351
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
  • Loading branch information
rluvaton authored and alexfernandez committed Nov 1, 2023
1 parent af0fe08 commit 3b61885
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions benchmark/streams/readable-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 3b61885

Please sign in to comment.