• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const { EventEmitter } = require('events');
4
5const bench = common.createBenchmark(main, {
6  newListener: [0, 1],
7  removeListener: [0, 1],
8  n: [1e6],
9});
10
11function main({ newListener, removeListener, n }) {
12  const ee = new EventEmitter();
13  const listeners = [];
14
15  for (let k = 0; k < 10; k += 1)
16    listeners.push(() => {});
17
18  if (newListener === 1)
19    ee.on('newListener', (event, listener) => {});
20
21  if (removeListener === 1)
22    ee.on('removeListener', (event, listener) => {});
23
24  bench.start();
25  for (let i = 0; i < n; i += 1) {
26    const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
27    for (let k = listeners.length; --k >= 0; /* empty */) {
28      ee.on(dummy, listeners[k]);
29    }
30    for (let k = listeners.length; --k >= 0; /* empty */) {
31      ee.removeListener(dummy, listeners[k]);
32    }
33  }
34  bench.end(n);
35}
36