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:
Miško Hevery
2017-07-13 15:36:11 -07:00
committed by Miško Hevery
parent 8bcb268140
commit 6279e50d78
11 changed files with 122 additions and 42 deletions

View File

@ -172,7 +172,8 @@ class DefaultServerRenderer2 implements Renderer2 {
const el =
typeof target === 'string' ? getDOM().getGlobalEventTarget(this.document, target) : target;
const outsideHandler = (event: any) => this.ngZone.runGuarded(() => callback(event));
return this.ngZone.runOutsideAngular(() => getDOM().onAndCancel(el, eventName, outsideHandler));
return this.ngZone.runOutsideAngular(
() => getDOM().onAndCancel(el, eventName, outsideHandler) as any);
}
}