Skip to content

Commit 4f84cdc

Browse files
ronagruyadorno
authored andcommitted
events: optimize EventTarget.addEventListener
PR-URL: #55312 Fixes: #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>
1 parent 63bc405 commit 4f84cdc

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
n: [1e5],
6+
nListener: [1, 5, 10],
7+
});
8+
9+
function main({ n, nListener }) {
10+
const target = new EventTarget();
11+
const listeners = [];
12+
for (let k = 0; k < nListener; k += 1)
13+
listeners.push(() => {});
14+
15+
bench.start();
16+
for (let i = 0; i < n; i += 1) {
17+
for (let k = listeners.length; --k >= 0;) {
18+
target.addEventListener('abort', listeners[k]);
19+
}
20+
for (let k = listeners.length; --k >= 0;) {
21+
target.removeEventListener('abort', listeners[k]);
22+
}
23+
}
24+
bench.end(n);
25+
}

benchmark/events/eventtarget-add-remove.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
5-
n: [1e6],
6-
nListener: [5, 10],
5+
n: [1e5],
6+
nListener: [1, 5, 10],
77
});
88

99
function main({ n, nListener }) {

lib/internal/event_target.js

+35-24
Original file line numberDiff line numberDiff line change
@@ -597,19 +597,40 @@ class EventTarget {
597597
if (arguments.length < 2)
598598
throw new ERR_MISSING_ARGS('type', 'listener');
599599

600-
// We validateOptions before the validateListener check because the spec
601-
// requires us to hit getters.
602-
const {
603-
once,
604-
capture,
605-
passive,
606-
signal,
607-
isNodeStyleListener,
608-
weak,
609-
resistStopPropagation,
610-
} = validateEventListenerOptions(options);
611-
612-
validateAbortSignal(signal, 'options.signal');
600+
let once = false;
601+
let capture = false;
602+
let passive = false;
603+
let isNodeStyleListener = false;
604+
let weak = false;
605+
let resistStopPropagation = false;
606+
607+
if (options !== kEmptyObject) {
608+
// We validateOptions before the validateListener check because the spec
609+
// requires us to hit getters.
610+
options = validateEventListenerOptions(options);
611+
612+
once = options.once;
613+
capture = options.capture;
614+
passive = options.passive;
615+
isNodeStyleListener = options.isNodeStyleListener;
616+
weak = options.weak;
617+
resistStopPropagation = options.resistStopPropagation;
618+
619+
const signal = options.signal;
620+
621+
validateAbortSignal(signal, 'options.signal');
622+
623+
if (signal) {
624+
if (signal.aborted) {
625+
return;
626+
}
627+
// TODO(benjamingr) make this weak somehow? ideally the signal would
628+
// not prevent the event target from GC.
629+
signal.addEventListener('abort', () => {
630+
this.removeEventListener(type, listener, options);
631+
}, { __proto__: null, once: true, [kWeakHandler]: this, [kResistStopPropagation]: true });
632+
}
633+
}
613634

614635
if (!validateEventListener(listener)) {
615636
// The DOM silently allows passing undefined as a second argument
@@ -623,18 +644,8 @@ class EventTarget {
623644
process.emitWarning(w);
624645
return;
625646
}
626-
type = webidl.converters.DOMString(type);
627647

628-
if (signal) {
629-
if (signal.aborted) {
630-
return;
631-
}
632-
// TODO(benjamingr) make this weak somehow? ideally the signal would
633-
// not prevent the event target from GC.
634-
signal.addEventListener('abort', () => {
635-
this.removeEventListener(type, listener, options);
636-
}, { __proto__: null, once: true, [kWeakHandler]: this, [kResistStopPropagation]: true });
637-
}
648+
type = webidl.converters.DOMString(type);
638649

639650
let root = this[kEvents].get(type);
640651

0 commit comments

Comments
 (0)