Should I use onPush on zoneless angular application?

Viewed 26

If I disable zonejs from Angular application, do I still need to set components as OnPush for performance?

1 Answers

Disabling zoneJS means that your component is isolated from the change detection of the whole application. Angular works with ZoneJS internally, to detect when to update your UI.

By disabling zoneJS, you disable all internal emitters.

Since it's a very particular use-case, you should check that for yourself : first, try adding the common lifecycle hooks, see if they still trigger. Those are ngOnInit, ngAfterViewInit, etc.

Put a console.log into them, if it triggers, then it means there's still some part of the app that gets triggered (which is good).

Next, try with ngDoCheck : this lifecycle hook gets triggered everytime Angular has to update the UI. By putting a console.log into it, you will be able to see how many times the change detection gets triggered. If it's too much for your taste, then yeah, you will probably need to add onPush to your component. If not, then you should be good !

I would also suggest to try the async pipe since it's one of the most used, it would be pretty bad to add a tap(() => this.cdRef.detectChanges()) to each observable in my opinion ...

Related