• 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: [5e6],
7  listeners: [5, 50],
8  raw: ['true', 'false'],
9});
10
11function main({ n, listeners, raw }) {
12  const ee = new EventEmitter();
13  ee.setMaxListeners(listeners * 2 + 1);
14
15  for (let k = 0; k < listeners; k += 1) {
16    ee.on('dummy0', () => {});
17    ee.once('dummy1', () => {});
18  }
19
20  if (raw === 'true') {
21    bench.start();
22    for (let i = 0; i < n; i += 1) {
23      const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
24      ee.rawListeners(dummy);
25    }
26    bench.end(n);
27  } else {
28    bench.start();
29    for (let i = 0; i < n; i += 1) {
30      const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
31      ee.listeners(dummy);
32    }
33    bench.end(n);
34  }
35}
36