• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3
4const bench = common.createBenchmark(main, {
5  n: [5e6],
6  type: ['depth', 'depth1', 'breadth', 'breadth1', 'breadth4', 'clear'],
7});
8
9function main({ n, type }) {
10  switch (type) {
11    case 'depth':
12      depth(n);
13      break;
14    case 'depth1':
15      depth1(n);
16      break;
17    case 'breadth':
18      breadth(n);
19      break;
20    case 'breadth1':
21      breadth1(n);
22      break;
23    case 'breadth4':
24      breadth4(n);
25      break;
26    case 'clear':
27      clear(n);
28      break;
29  }
30}
31
32// setImmediate tail recursion, 0 arguments
33function depth(N) {
34  let n = 0;
35  bench.start();
36  setImmediate(cb);
37  function cb() {
38    n++;
39    if (n === N)
40      bench.end(n);
41    else
42      setImmediate(cb);
43  }
44}
45
46// setImmediate tail recursion, 1 argument
47function depth1(N) {
48  let n = 0;
49  bench.start();
50  setImmediate(cb, 1);
51  function cb(a1) {
52    n++;
53    if (n === N)
54      bench.end(N);
55    else
56      setImmediate(cb, 1);
57  }
58}
59
60// Concurrent setImmediate, 0 arguments
61function breadth(N) {
62  let n = 0;
63  bench.start();
64  function cb() {
65    n++;
66    if (n === N)
67      bench.end(N);
68  }
69  for (let i = 0; i < N; i++) {
70    setImmediate(cb);
71  }
72}
73
74// Concurrent setImmediate, 1 argument
75function breadth1(N) {
76  let n = 0;
77  bench.start();
78  function cb(a1) {
79    n++;
80    if (n === N)
81      bench.end(n);
82  }
83  for (let i = 0; i < N; i++) {
84    setImmediate(cb, 1);
85  }
86}
87
88// Concurrent setImmediate, 4 arguments
89function breadth4(N) {
90  N /= 2;
91  let n = 0;
92  bench.start();
93  function cb(a1, a2, a3, a4) {
94    n++;
95    if (n === N)
96      bench.end(n);
97  }
98  for (let i = 0; i < N; i++) {
99    setImmediate(cb, 1, 2, 3, 4);
100  }
101}
102
103function clear(N) {
104  N *= 4;
105  bench.start();
106  function cb(a1) {
107    if (a1 === 2)
108      bench.end(N);
109  }
110  for (let i = 0; i < N; i++) {
111    clearImmediate(setImmediate(cb, 1));
112  }
113  setImmediate(cb, 2);
114}
115