From 760a35144f1a0f91ecacccb94a80f2506ee186d3 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Sun, 26 Feb 2023 11:00:26 +0530 Subject: [PATCH] benchmark: add benchmark for EventTarget add and remove Refs: https://github.com/nodejs/performance/issues/60 PR-URL: https://github.com/nodejs/node/pull/46779 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- benchmark/events/eventtarget-add-remove.js | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 benchmark/events/eventtarget-add-remove.js diff --git a/benchmark/events/eventtarget-add-remove.js b/benchmark/events/eventtarget-add-remove.js new file mode 100644 index 00000000000000..a3defce03cfb8d --- /dev/null +++ b/benchmark/events/eventtarget-add-remove.js @@ -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); +}