Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f58d743

Browse filesBrowse files
committed
fix: optimise syntax used within handleEvent()
1 parent 40db84e commit f58d743
Copy full SHA for f58d743

File tree

1 file changed

+17
-2
lines changed
Filter options

1 file changed

+17
-2
lines changed

‎packages/core/data/dom-events/dom-event.ts

Copy file name to clipboardExpand all lines: packages/core/data/dom-events/dom-event.ts
+17-2Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { EventData, ListenerEntry, Observable } from '../observable/index';
22
import type { ViewBase } from '../../ui/core/view-base';
33

4+
// This file contains some of Core's hot paths, so attention has been taken to
5+
// optimise it. Where specified, optimisations made have been informed based on
6+
// profiles taken on an Apple M1 Max in a debug build on @nativescript/ios@8.3.3
7+
// on an iOS Simulator.
8+
49
const timeOrigin = Date.now();
510

611
/**
@@ -338,11 +343,21 @@ export class DOMEvent implements Event {
338343
private handleEvent({ data, isGlobal, getListenersForType, phase, removeEventListener }: { data: EventData; isGlobal: boolean; getListenersForType: () => readonly ListenerEntry[]; phase: 0 | 1 | 2 | 3; removeEventListener: (eventName: string, callback?: any, thisArg?: any, capture?: boolean) => void }) {
339344
// Work on a copy of the array, as any callback could modify the
340345
// original array during the loop.
341-
const listenersForTypeCopy = getListenersForType().slice();
346+
//
347+
// Cloning the array via spread syntax is up to 180 nanoseconds faster
348+
// per run than using Array.prototype.slice().
349+
const listenersForTypeCopy = [...getListenersForType()];
342350

343351
for (let i = listenersForTypeCopy.length - 1; i >= 0; i--) {
344352
const listener = listenersForTypeCopy[i];
345-
const { callback, capture, thisArg, once, passive } = listener;
353+
354+
// Assigning variables this old-fashioned way is up to 50
355+
// nanoseconds faster per run than ESM destructuring syntax.
356+
const callback = listener.callback;
357+
const capture = listener.capture;
358+
const thisArg = listener.thisArg;
359+
const once = listener.once;
360+
const passive = listener.once;
346361

347362
// The event listener may have been removed since we took a copy of
348363
// the array, so bail out if so.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.