angularangular18

Do I still need OnPush if my app is Zoneless?


I have migrated my app to zoneless thanks to provideExperimentalZonelessChangeDetection() and having a mix of signals and Observables +AsyncPipe.

Do I still need the OnPush ChangeDetection Strategy ?


Solution

  • TL;DR

    Yes.
    Just as with zone-based change detection, it prevents your components from being checked if it's not needed, and thus increases the performance of each CD.


    Thorough explanation

    Components using the OnPush change detection strategy will be checked by change detection if the parent was checked and if either:

    We can say the OnPush strategy decides which component will be checked by CD.

    Angular apps also need to decide when the tick is fired from the ApplicationRef. This is what we call scheduling. When is CD actually starting?

    In Zone apps, Zone.js is the scheduler by the means of patching all the async APIs (setTimeout(), Promise, addEventListener(), etc). When one of those is called, a CD is scheduled.

    In zoneless apps, this is no longer possible as no APIs are monkey patched. The framework needs to find another way to schedule CD. Today it uses following:

    To sum-up:

    Also to make things clear, OnPush is not the default when using Zoneless scheduling.