1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

perf(ext/web): optimize EventTarget (#12166)

and all its subclasses including `AbortSignal` ...

Instead of storing associated data in a global `WeakMap` we store them as private attributes (via a Symbol) on the object instances
This commit is contained in:
Aaron O'Mullan 2021-09-21 17:51:44 +02:00 committed by GitHub
parent f827b93df4
commit 0aa6fefce7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,9 +34,6 @@
SymbolFor, SymbolFor,
SymbolToStringTag, SymbolToStringTag,
TypeError, TypeError,
WeakMap,
WeakMapPrototypeGet,
WeakMapPrototypeSet,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
// accessors for non runtime visible data // accessors for non runtime visible data
@ -842,32 +839,30 @@
// Accessors for non-public data // Accessors for non-public data
const eventTargetData = new WeakMap(); const eventTargetData = Symbol();
function setEventTargetData(value) { function setEventTargetData(target) {
WeakMapPrototypeSet(eventTargetData, value, getDefaultTargetData()); target[eventTargetData] = getDefaultTargetData();
} }
function getAssignedSlot(target) { function getAssignedSlot(target) {
return Boolean(WeakMapPrototypeGet(eventTargetData, target)?.assignedSlot); return Boolean(target?.[eventTargetData]?.assignedSlot);
} }
function getHasActivationBehavior(target) { function getHasActivationBehavior(target) {
return Boolean( return Boolean(target?.[eventTargetData]?.hasActivationBehavior);
WeakMapPrototypeGet(eventTargetData, target)?.hasActivationBehavior,
);
} }
function getHost(target) { function getHost(target) {
return WeakMapPrototypeGet(eventTargetData, target)?.host ?? null; return target?.[eventTargetData]?.host ?? null;
} }
function getListeners(target) { function getListeners(target) {
return WeakMapPrototypeGet(eventTargetData, target)?.listeners ?? {}; return target?.[eventTargetData]?.listeners ?? {};
} }
function getMode(target) { function getMode(target) {
return WeakMapPrototypeGet(eventTargetData, target)?.mode ?? null; return target?.[eventTargetData]?.mode ?? null;
} }
function getDefaultTargetData() { function getDefaultTargetData() {
@ -882,7 +877,7 @@
class EventTarget { class EventTarget {
constructor() { constructor() {
WeakMapPrototypeSet(eventTargetData, this, getDefaultTargetData()); this[eventTargetData] = getDefaultTargetData();
} }
addEventListener( addEventListener(
@ -898,10 +893,7 @@
} }
options = normalizeAddEventHandlerOptions(options); options = normalizeAddEventHandlerOptions(options);
const { listeners } = WeakMapPrototypeGet( const { listeners } = (this ?? globalThis)[eventTargetData];
eventTargetData,
this ?? globalThis,
);
if (!(type in listeners)) { if (!(type in listeners)) {
listeners[type] = []; listeners[type] = [];
@ -946,8 +938,7 @@
prefix: "Failed to execute 'removeEventListener' on 'EventTarget'", prefix: "Failed to execute 'removeEventListener' on 'EventTarget'",
}); });
const listeners = const { listeners } = (this ?? globalThis)[eventTargetData];
WeakMapPrototypeGet(eventTargetData, this ?? globalThis).listeners;
if (callback !== null && type in listeners) { if (callback !== null && type in listeners) {
listeners[type] = ArrayPrototypeFilter( listeners[type] = ArrayPrototypeFilter(
listeners[type], listeners[type],
@ -980,7 +971,7 @@
}); });
const self = this ?? globalThis; const self = this ?? globalThis;
const listeners = WeakMapPrototypeGet(eventTargetData, self).listeners; const { listeners } = self[eventTargetData];
if (!(event.type in listeners)) { if (!(event.type in listeners)) {
setTarget(event, this); setTarget(event, this);
return true; return true;