summaryrefslogtreecommitdiff
path: root/benchmark/streams/readable-async-iterator.js
blob: e8e8355444069a06ec8e12d1d4d42dd8526ab8d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';

const common = require('../common');
const Readable = require('stream').Readable;

const bench = common.createBenchmark(main, {
  n: [1e5],
  sync: ['yes', 'no'],
});

async function main({ n, sync }) {
  sync = sync === 'yes';

  const s = new Readable({
    objectMode: true,
    read() {
      if (sync) {
        this.push(1);
      } else {
        process.nextTick(() => {
          this.push(1);
        });
      }
    },
  });

  bench.start();

  let x = 0;
  for await (const chunk of s) {
    x += chunk;
    if (x > n) {
      break;
    }
  }

  // Use x to ensure V8 does not optimize away the loop as a noop.
  console.assert(x);

  bench.end(n);
}