Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ext/event): replace ReflectHas with object lookup #20190

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions ext/web/02_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const {
ObjectGetOwnPropertyDescriptor,
ObjectPrototypeIsPrototypeOf,
ReflectDefineProperty,
ReflectHas,
SafeArrayIterator,
SafeMap,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -108,14 +107,6 @@ function setStopImmediatePropagation(
event[_stopImmediatePropagationFlag] = value;
}

// Type guards that widen the event type

function hasRelatedTarget(
event,
) {
return ReflectHas(event, "relatedTarget");
}

const isTrusted = ObjectGetOwnPropertyDescriptor({
get isTrusted() {
return this[_isTrusted];
Expand Down Expand Up @@ -501,9 +492,12 @@ function isShadowRoot(nodeImpl) {
}

function isSlottable(
nodeImpl,
/* nodeImpl, */
) {
return Boolean(isNode(nodeImpl) && ReflectHas(nodeImpl, "assignedSlot"));
// TODO(marcosc90) currently there aren't any slottables nodes
// https://dom.spec.whatwg.org/#concept-slotable
// return isNode(nodeImpl) && ReflectHas(nodeImpl, "assignedSlot");
return false;
}

// DOM Logic functions
Expand Down Expand Up @@ -546,9 +540,7 @@ function dispatch(
setDispatched(eventImpl, true);

targetOverride = targetOverride ?? targetImpl;
const eventRelatedTarget = hasRelatedTarget(eventImpl)
? eventImpl.relatedTarget
: null;
const eventRelatedTarget = eventImpl.relatedTarget;
let relatedTarget = retarget(eventRelatedTarget, targetImpl);

if (targetImpl !== relatedTarget || targetImpl === eventRelatedTarget) {
Expand Down Expand Up @@ -972,7 +964,7 @@ class EventTarget {

const { listeners } = self[eventTargetData];

if (!(ReflectHas(listeners, type))) {
if (!listeners[type]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, use in

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self[eventTargetData] is only accessible internally so we can probably use whatever benchmarks fastest here. There shouldn't be a risk of improper boolean coercion if we know the field is null | array.

Copy link
Contributor Author

@marcosc90 marcosc90 Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listeners is an internal value which is only set by us, and listeners[type] it's either undefined or []. There's no change in using object lookup or in, and since we have a lint rule against in I used object lookup.

listeners[type] = [];
}

Expand Down Expand Up @@ -1020,7 +1012,7 @@ class EventTarget {
);

const { listeners } = self[eventTargetData];
if (callback !== null && ReflectHas(listeners, type)) {
if (callback !== null && listeners[type]) {
listeners[type] = ArrayPrototypeFilter(
listeners[type],
(listener) => listener.callback !== callback,
Expand Down Expand Up @@ -1069,7 +1061,7 @@ class EventTarget {
}

const { listeners } = self[eventTargetData];
if (!ReflectHas(listeners, event.type)) {
if (!listeners[event.type]) {
setTarget(event, this);
return true;
}
Expand Down