perf(core): use native addEventListener for faster rendering. (#18107)
Angular can make many assumptions about its event handlers. As a result the bookkeeping for native addEventListener is significantly cheaper than Zone's addEventLister which can't make such assumptions. This change bypasses the Zone's addEventListener if present and always uses the native addEventHandler. As a result registering event listeners is about 3 times faster. PR Close #18107
This commit is contained in:

committed by
Miško Hevery

parent
8bcb268140
commit
6279e50d78
@ -189,14 +189,7 @@ export function listenToElementOutputs(view: ViewData, compView: ViewData, def:
|
||||
}
|
||||
|
||||
function renderEventHandlerClosure(view: ViewData, index: number, eventName: string) {
|
||||
return (event: any) => {
|
||||
try {
|
||||
return dispatchEvent(view, index, eventName, event);
|
||||
} catch (e) {
|
||||
// Attention: Don't rethrow, to keep in sync with directive events.
|
||||
view.root.errorHandler.handleError(e);
|
||||
}
|
||||
}
|
||||
return (event: any) => dispatchEvent(view, index, eventName, event);
|
||||
}
|
||||
|
||||
|
||||
|
@ -138,14 +138,7 @@ export function createDirectiveInstance(view: ViewData, def: NodeDef): any {
|
||||
}
|
||||
|
||||
function eventHandlerClosure(view: ViewData, index: number, eventName: string) {
|
||||
return (event: any) => {
|
||||
try {
|
||||
return dispatchEvent(view, index, eventName, event);
|
||||
} catch (e) {
|
||||
// Attention: Don't rethrow, as it would cancel Observable subscriptions!
|
||||
view.root.errorHandler.handleError(e);
|
||||
}
|
||||
}
|
||||
return (event: any) => dispatchEvent(view, index, eventName, event);
|
||||
}
|
||||
|
||||
export function checkAndUpdateDirectiveInline(
|
||||
|
@ -126,12 +126,18 @@ export function markParentViewsForCheckProjectedViews(view: ViewData, endView: V
|
||||
}
|
||||
|
||||
export function dispatchEvent(
|
||||
view: ViewData, nodeIndex: number, eventName: string, event: any): boolean {
|
||||
const nodeDef = view.def.nodes[nodeIndex];
|
||||
const startView =
|
||||
nodeDef.flags & NodeFlags.ComponentView ? asElementData(view, nodeIndex).componentView : view;
|
||||
markParentViewsForCheck(startView);
|
||||
return Services.handleEvent(view, nodeIndex, eventName, event);
|
||||
view: ViewData, nodeIndex: number, eventName: string, event: any): boolean|undefined {
|
||||
try {
|
||||
const nodeDef = view.def.nodes[nodeIndex];
|
||||
const startView = nodeDef.flags & NodeFlags.ComponentView ?
|
||||
asElementData(view, nodeIndex).componentView :
|
||||
view;
|
||||
markParentViewsForCheck(startView);
|
||||
return Services.handleEvent(view, nodeIndex, eventName, event);
|
||||
} catch (e) {
|
||||
// Attention: Don't rethrow, as it would cancel Observable subscriptions!
|
||||
view.root.errorHandler.handleError(e);
|
||||
}
|
||||
}
|
||||
|
||||
export function declaredViewContainer(view: ViewData): ElementData|null {
|
||||
|
@ -164,13 +164,39 @@ export class NgZone {
|
||||
*
|
||||
* If a synchronous error happens it will be rethrown and not reported via `onError`.
|
||||
*/
|
||||
run(fn: () => any): any { return (this as any as NgZonePrivate)._inner.run(fn); }
|
||||
run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
|
||||
return (this as any as NgZonePrivate)._inner.run(fn, applyThis, applyArgs) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the `fn` function synchronously within the Angular zone as a task and returns value
|
||||
* returned by the function.
|
||||
*
|
||||
* Running functions via `run` allows you to reenter Angular zone from a task that was executed
|
||||
* outside of the Angular zone (typically started via {@link #runOutsideAngular}).
|
||||
*
|
||||
* Any future tasks or microtasks scheduled from within this function will continue executing from
|
||||
* within the Angular zone.
|
||||
*
|
||||
* If a synchronous error happens it will be rethrown and not reported via `onError`.
|
||||
*/
|
||||
runTask<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[], name?: string): T {
|
||||
const zone = (this as any as NgZonePrivate)._inner;
|
||||
const task = zone.scheduleEventTask('NgZoneEvent: ' + name, fn, EMPTY_PAYLOAD, noop, noop);
|
||||
try {
|
||||
return zone.runTask(task, applyThis, applyArgs) as T;
|
||||
} finally {
|
||||
zone.cancelTask(task);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as `run`, except that synchronous errors are caught and forwarded via `onError` and not
|
||||
* rethrown.
|
||||
*/
|
||||
runGuarded(fn: () => any): any { return (this as any as NgZonePrivate)._inner.runGuarded(fn); }
|
||||
runGuarded<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T {
|
||||
return (this as any as NgZonePrivate)._inner.runGuarded(fn, applyThis, applyArgs) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the `fn` function synchronously in Angular's parent zone and returns value returned by
|
||||
@ -185,9 +211,15 @@ export class NgZone {
|
||||
*
|
||||
* Use {@link #run} to reenter the Angular zone and do work that updates the application model.
|
||||
*/
|
||||
runOutsideAngular(fn: () => any): any { return (this as any as NgZonePrivate)._outer.run(fn); }
|
||||
runOutsideAngular<T>(fn: (...args: any[]) => T): T {
|
||||
return (this as any as NgZonePrivate)._outer.run(fn) as T;
|
||||
}
|
||||
}
|
||||
|
||||
function noop(){};
|
||||
const EMPTY_PAYLOAD = {};
|
||||
|
||||
|
||||
interface NgZonePrivate extends NgZone {
|
||||
_outer: Zone;
|
||||
_inner: Zone;
|
||||
|
Reference in New Issue
Block a user