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