1
1
import type { EventData , ListenerEntry , Observable } from '../observable/index' ;
2
2
import type { ViewBase } from '../../ui/core/view-base' ;
3
3
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
+
4
9
const timeOrigin = Date . now ( ) ;
5
10
6
11
/**
@@ -338,11 +343,21 @@ export class DOMEvent implements Event {
338
343
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 } ) {
339
344
// Work on a copy of the array, as any callback could modify the
340
345
// 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 ( ) ] ;
342
350
343
351
for ( let i = listenersForTypeCopy . length - 1 ; i >= 0 ; i -- ) {
344
352
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 ;
346
361
347
362
// The event listener may have been removed since we took a copy of
348
363
// the array, so bail out if so.
0 commit comments