Skip to content

Commit

Permalink
benchmark: add benchmark for EventTarget add and remove
Browse files Browse the repository at this point in the history
Refs: nodejs/performance#60
PR-URL: #46779
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
debadree25 authored and targos committed Mar 14, 2023
1 parent f4cbe4e commit 760a351
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions benchmark/events/eventtarget-add-remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common.js');

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

function main({ n, nListener }) {
const target = new EventTarget();
const listeners = [];
for (let k = 0; k < nListener; k += 1)
listeners.push(() => {});

bench.start();
for (let i = 0; i < n; i += 1) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
for (let k = listeners.length; --k >= 0;) {
target.addEventListener(dummy, listeners[k]);
}
for (let k = listeners.length; --k >= 0;) {
target.removeEventListener(dummy, listeners[k]);
}
}
bench.end(n);
}

0 comments on commit 760a351

Please sign in to comment.