Do I still need OnPush if my app is Zoneless?
Asked Answered
B

1

12

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 ?

Breann answered 4/6, 2024 at 19:55 Comment(0)
B
21

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:

  • The component was marked dirty (via markForCheck()/AsyncPipe)
  • One of the input references changed
  • An event listener in the template fires

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:

  • Signal updates (set() or update())
  • markForCheck() or AsyncPipe
  • An event listener in the template fires

To sum-up:

  • Zoneless scheduling is about when components are checked
  • OnPush is about which component is checked

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

Breann answered 4/6, 2024 at 19:55 Comment(3)
Thanx for the explanation πŸ‘ŒπŸΌ – Bracing
Asking a question and answering it at the exact same time πŸ€” too big an ego (French... could be so... 😜)? – Bracing
I saw that question way to often on multiple platform. SO is the best place to terms of indexation to help people find this answer :) – Breann

© 2022 - 2025 β€” McMap. All rights reserved.