• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const EventEmitter = require('events').EventEmitter;
4
5const bench = common.createBenchmark(main, {
6  n: [2e7],
7  argc: [0, 1, 4, 5]
8});
9
10function main({ n, argc }) {
11  const ee = new EventEmitter();
12
13  function listener() {}
14
15  switch (argc) {
16    case 0:
17      bench.start();
18      for (let i = 0; i < n; i += 1) {
19        const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
20        ee.once(dummy, listener);
21        ee.emit(dummy);
22      }
23      bench.end(n);
24      break;
25    case 1:
26      bench.start();
27      for (let i = 0; i < n; i += 1) {
28        const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
29        ee.once(dummy, listener);
30        ee.emit(dummy, n);
31      }
32      bench.end(n);
33      break;
34    case 4:
35      bench.start();
36      for (let i = 0; i < n; i += 1) {
37        const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
38        ee.once(dummy, listener);
39        ee.emit(dummy, 'foo', argc, 'bar', false);
40      }
41      bench.end(n);
42      break;
43    case 5:
44      bench.start();
45      for (let i = 0; i < n; i += 1) {
46        const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
47        ee.once(dummy, listener);
48        ee.emit(dummy, true, 7.5, 'foo', null, n);
49      }
50      bench.end(n);
51      break;
52    default:
53      throw new Error('Unsupported argument count');
54  }
55}
56