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 <[email protected]>
PR-URL: #44657
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
  • Loading branch information
anonrig and mscdex committed Oct 14, 2022
1 parent b126f39 commit 267a03e
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 267a03e

Please sign in to comment.