seescode
seescode

Reputation: 2131

In what cases should I use runOutsideAngular if I'm already using OnPush change detection

I'm already using OnPush change detection with immutable data. I'm wondering is there further optimizations I can do with my change detection by using ngZone.runOutsideAngular? I'm just looking for some guidelines here.

Upvotes: 13

Views: 2896

Answers (1)

Max Koretskyi
Max Koretskyi

Reputation: 105439

They are not particularly related. OnPush controls change detection on the level of each component, while ngZone "sort of" triggers change detection for the entire application.

Angular uses zones, particularly NgZone to get notified whenever there are no more tasks. A task scheduled in a zone will be executed in this zone. So all async tasks like setTimeout are executed inside NgZone. The ngZone.runOutsideAngular allows you to schedule a task outside ngZone which means that once it's complete Angular will not be notified and no change detection will take place.

If you have some recurring async task that gets executed many times a second (like a mousemove event) then you could probably leverage ngZone.runOutsideAngular to avoid triggering change detection for each event. Then you could schedule a manual change detection once a second. Other than that I can't think of how it can help you optimize the application.

Upvotes: 15

Related Questions