• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const Readable = require('stream').Readable;
5
6const bench = common.createBenchmark(main, {
7  n: [1e5],
8  sync: ['yes', 'no'],
9});
10
11async function main({ n, sync }) {
12  sync = sync === 'yes';
13
14  const s = new Readable({
15    objectMode: true,
16    read() {
17      if (sync) {
18        this.push(1);
19      } else {
20        process.nextTick(() => {
21          this.push(1);
22        });
23      }
24    },
25  });
26
27  bench.start();
28
29  let x = 0;
30  for await (const chunk of s) {
31    x += chunk;
32    if (x > n) {
33      break;
34    }
35  }
36
37  // Use x to ensure V8 does not optimize away the loop as a noop.
38  console.assert(x);
39
40  bench.end(n);
41}
42