Angular NgZone.runOutsideAngular and OnPush change detection strategy
Asked Answered
M

1

7

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)

Mcmahan answered 1/4, 2020 at 11:17 Comment(2)
Do you have some decision? I am also interested in it...Voyeur
@Voyeur added my thoughts as an answerMcmahan
M
1

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.

Mcmahan answered 18/11, 2020 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.