1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

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

This PR optimizes event dispatch by replacing `ReflectHas` with object
lookup. I also made `isSlottable` return `false` since AFAIK there
aren't any slottables nodes in Deno

**This PR**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark            time (avg)        iter/s             (min … max)       p75       p99      p995
--------------------------------------------------------------------- -----------------------------
event dispatch       80.46 ns/iter  12,428,739.4  (73.84 ns … 120.07 ns)  81.82 ns  86.34 ns  91.18 ns
```

**main**

```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark            time (avg)        iter/s             (min … max)       p75       p99      p995
--------------------------------------------------------------------- -----------------------------
event dispatch      102.66 ns/iter   9,741,319.6  (96.66 ns … 132.88 ns) 104.18 ns 114.58 ns 118.45 ns
```

```js
const tg = new EventTarget();
const ev = new Event("foo");

const listener = () => {};
tg.addEventListener("foo", listener);

Deno.bench("event dispatch ", () => {
  tg.dispatchEvent(ev);
});
```

towards https://github.com/denoland/deno/issues/20167
This commit is contained in:
Marcos Casagrande 2023-08-18 14:44:57 +02:00 committed by GitHub
parent a48ec1d563
commit e1aa514179
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -31,7 +31,6 @@ const {
ObjectGetOwnPropertyDescriptor, ObjectGetOwnPropertyDescriptor,
ObjectPrototypeIsPrototypeOf, ObjectPrototypeIsPrototypeOf,
ReflectDefineProperty, ReflectDefineProperty,
ReflectHas,
SafeArrayIterator, SafeArrayIterator,
SafeMap, SafeMap,
StringPrototypeStartsWith, StringPrototypeStartsWith,
@ -108,14 +107,6 @@ function setStopImmediatePropagation(
event[_stopImmediatePropagationFlag] = value; event[_stopImmediatePropagationFlag] = value;
} }
// Type guards that widen the event type
function hasRelatedTarget(
event,
) {
return ReflectHas(event, "relatedTarget");
}
const isTrusted = ObjectGetOwnPropertyDescriptor({ const isTrusted = ObjectGetOwnPropertyDescriptor({
get isTrusted() { get isTrusted() {
return this[_isTrusted]; return this[_isTrusted];
@ -501,9 +492,12 @@ function isShadowRoot(nodeImpl) {
} }
function isSlottable( 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 // DOM Logic functions
@ -546,9 +540,7 @@ function dispatch(
setDispatched(eventImpl, true); setDispatched(eventImpl, true);
targetOverride = targetOverride ?? targetImpl; targetOverride = targetOverride ?? targetImpl;
const eventRelatedTarget = hasRelatedTarget(eventImpl) const eventRelatedTarget = eventImpl.relatedTarget;
? eventImpl.relatedTarget
: null;
let relatedTarget = retarget(eventRelatedTarget, targetImpl); let relatedTarget = retarget(eventRelatedTarget, targetImpl);
if (targetImpl !== relatedTarget || targetImpl === eventRelatedTarget) { if (targetImpl !== relatedTarget || targetImpl === eventRelatedTarget) {
@ -972,7 +964,7 @@ class EventTarget {
const { listeners } = self[eventTargetData]; const { listeners } = self[eventTargetData];
if (!(ReflectHas(listeners, type))) { if (!listeners[type]) {
listeners[type] = []; listeners[type] = [];
} }
@ -1020,7 +1012,7 @@ class EventTarget {
); );
const { listeners } = self[eventTargetData]; const { listeners } = self[eventTargetData];
if (callback !== null && ReflectHas(listeners, type)) { if (callback !== null && listeners[type]) {
listeners[type] = ArrayPrototypeFilter( listeners[type] = ArrayPrototypeFilter(
listeners[type], listeners[type],
(listener) => listener.callback !== callback, (listener) => listener.callback !== callback,
@ -1069,7 +1061,7 @@ class EventTarget {
} }
const { listeners } = self[eventTargetData]; const { listeners } = self[eventTargetData];
if (!ReflectHas(listeners, event.type)) { if (!listeners[event.type]) {
setTarget(event, this); setTarget(event, this);
return true; return true;
} }