Skip to content

Commit a41b0e1

Browse files
ronagmarco-ippolito
authored andcommitted
events: optimize EventTarget.addEventListener
PR-URL: #55312 Fixes: #55311 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 5489656 commit a41b0e1

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
@@ -601,19 +601,40 @@ class EventTarget {
601601
if (arguments.length < 2)
602602
throw new ERR_MISSING_ARGS('type', 'listener');
603603

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

618639
if (!validateEventListener(listener)) {
619640
// The DOM silently allows passing undefined as a second argument
@@ -627,18 +648,8 @@ class EventTarget {
627648
process.emitWarning(w);
628649
return;
629650
}
630-
type = webidl.converters.DOMString(type);
631651

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

643654
let root = this[kEvents].get(type);
644655

0 commit comments

Comments
 (0)