• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3
4// The following benchmark measures setting up n * 1e6 timeouts,
5// as well as scheduling a next tick from each timeout. Those
6// then get executed on the next uv tick.
7
8const bench = common.createBenchmark(main, {
9  n: [5e4, 5e6],
10});
11
12function main({ n }) {
13  let count = 0;
14
15  // Function tracking on the hidden class in V8 can cause misleading
16  // results in this benchmark if only a single function is used —
17  // alternate between two functions for a fairer benchmark.
18
19  function cb() {
20    process.nextTick(counter);
21  }
22
23  function cb2() {
24    process.nextTick(counter);
25  }
26
27  function counter() {
28    count++;
29    if (count === n)
30      bench.end(n);
31  }
32
33  for (let i = 0; i < n; i++) {
34    setTimeout(i % 2 ? cb : cb2, 1);
35  }
36
37  bench.start();
38}
39