Skip to content

Commit

Permalink
events: add unique events benchmark
Browse files Browse the repository at this point in the history
Co-authored-by: Brian White <mscdex@mscdex.net>
PR-URL: #44657
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
2 people authored and RafaelGSS committed Nov 10, 2022
1 parent c45b6ae commit c3dd696
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions benchmark/events/ee-listen-unique.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const common = require('../common.js');
const { EventEmitter } = require('events');

const bench = common.createBenchmark(main, {
events: [1, 2, 3, 5, 10, 20],
n: [1e6],
});

function main({ events, n }) {
const ee = new EventEmitter();
const listeners = [];

for (let k = 0; k < 10; k += 1)
listeners.push(() => {});

const eventNames = [];
for (let k = 0; k < events; ++k)
eventNames.push(`dummy${k}`);

bench.start();
for (let i = 0; i < n; i += 1) {
for (const eventName of eventNames) {
for (let k = listeners.length; --k >= 0; /* empty */) {
ee.on(eventName, listeners[k]);
}
}
for (const eventName of eventNames) {
for (let k = listeners.length; --k >= 0; /* empty */) {
ee.removeListener(eventName, listeners[k]);
}
}
}
bench.end(n);
}

0 comments on commit c3dd696

Please sign in to comment.