0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-22 07:37:56 +01:00
nodejs/benchmark/events/eventtarget-add-remove.js
Robert Nagy 488ce99d76
events: optimize EventTarget.addEventListener
PR-URL: https://github.com/nodejs/node/pull/55312
Fixes: https://github.com/nodejs/node/issues/55311
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2024-10-14 10:24:32 +00:00

27 lines
652 B
JavaScript

'use strict';
const common = require('../common.js');
const bench = common.createBenchmark(main, {
n: [1e5],
nListener: [1, 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);
}