Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular NgZone.runOutsideAngular and OnPush change detection strategy

I'm trying to optimize the performance of code in angular application.

I know that I can runOutsideAngular some code in order not to cause change detection on each code run or event.

However, do I need to use runOutsideAngular when I utilize OnPush change detection strategy? (for listeners etc)

like image 564
Sergey Avatar asked Nov 16 '22 14:11

Sergey


1 Answers

I think I have an answer now.

In my opinion it depends on what an app looks like.

  • If it has lots of unoptimized components and the an async operation is going to fire lots of times or it's an event listener then it has to be within runOutsideAngular

  • However, if an application is mostly built with components with OnPush strategy or simply AppComponent uses OnPush (as whole tree is going to be using OnPush strategy defining it on a top level component defines this strategy for the whole application irreversibly) then you can omit using runOutsideAngular as triggers for change detection won't affect anything

Also I think that there is an option for fully OnPush optimized app to disable all events patches from Zone thus there would be no need for runOutsideAngular as all async events have no point of triggering NgZone.

like image 84
Sergey Avatar answered Dec 28 '22 09:12

Sergey