angular - Huge performance impact on Component's host document click (Angular2) -


so have 300 instantiations of component defines global click event listener through host property:

@component({   selector: 'value-edit',   template: ``,   host: {     "(document: click)": "onclickoff($event)",   },   changedetection: changedetectionstrategy.onpush ) export class valueeditcomponent {   onclickoff(globalevent) {     // make sure doesn't affect performance keep empty!   } } 

i noticed hugely impacts performance, takes 2-3 seconds of processing after every click everywhere on document.

this js cpu profile made in chrome sequence: wait ~5 seconds, click, wait few seconds , stop recording. click huge green column on screenshot:

single click on nothing reactive

i've tried detaching change detector on component or parent didn't help. commenting out line "(document: click)": "onclickoff($event)", fixes problem.

may issue of framework or bad usage i'm not sure how qualify or workaround in more good-practice-way.

plunker here

github issue here

solution / workaround

on angular 2.0.0 (final) code below result in same performance issue:

ngafterviewinit() {     document.addeventlistener('click', evt => this.evtclickhandler) } 

registering event outside "zone" should help:

constructor(zone: ngzone) { }  ngafterviewinit() {     this.zone.runoutsideangular(() => {         document.addeventlistener('click', evt => this.offclickhandler(evt))     }) } 

explanation

performance issue happened again me using sortablejs library. wasn't case before final version of angular changed due registering events on native elements.

for sortablejs library did this:

this.sortedimages = sortable.create(el, options) 

which resulted in bad performance while dragging elements:

enter image description here

solution or workaround goes this:

this.zone.runoutsideangular(() => {   this.sortedimages = sortable.create(el, options) }) 

where this.zone injected @angular/core/ngzone. way library registers event listeners outside of ngzone.

i have posted an issue on github problem recognized error on coding, not bug in angular. however, changes appeared between latest (rc - before final) versions.

so may bug or (latest) design don't have confirmation on this.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -