Sub topic 3
Why remove Zone.js
This chapter assumes you can build standalone components, use signal and computed, and know the default change detection model. It covers the reasons for dropping Zone.js. Later chapters cover how.
What the zone-based model costs
Zone.js is a library that patches browser APIs such as setTimeout, Promise, addEventListener and XMLHttpRequest. Angular runs your code inside a zone called NgZone. When any patched task finishes, the zone tells Angular, and Angular checks the component tree for changes.
The approach is convenient because you never tell Angular that something changed. The cost is that Angular cannot know what changed, only that something might have.
Reasons to remove it
Wasted checks
Zone.js reports every patched task, whether or not it touched state. A scroll or mousemove listener, a polling timer, or a third-party widget can each trigger a full pass. OnPush components skip their own work when their inputs are unchanged, but Angular still starts the pass from the root.
Bundle size and startup
Zone.js is a runtime dependency that ships to every user. Removing it makes the polyfill bundle smaller and removes the time spent patching APIs at startup.
Patching cannot cover everything
Zone.js can wrap Promise, but it cannot wrap native async/await, because the syntax does not go through the patched Promise constructor. To keep zones working, the Angular build compiles async/await down to Promises. A zoneless app does not need that downleveling and can ship the native syntax.
New browser APIs also need new patches. Until Zone.js supports an API, using it can cause missed updates or surprising extra passes.
Harder debugging
Stack traces in a zone-based app contain Zone.js frames, and the call chain through patched tasks is long. Without zones, traces are shorter and map more directly to your code.
Unpredictable updates
In a zone-based app, a view might update only because some unrelated timer happened to fire. Such bugs stay hidden until the timer is removed. In a zoneless app, a component updates only because something explicitly notified Angular, so you can trace each update to its cause.
What zoneless changes
Angular still has to learn that a change occurred. Without Zone.js, it relies on explicit signals from the framework:
| Question | Zone-based | Zoneless |
|---|---|---|
| Who notifies Angular? | Zone.js, after any patched task | Signals, `markForCheck`, the `async` pipe, template event listeners, and similar |
| What gets checked? | Whole tree, from the root | Components marked dirty |
| Cost of an unrelated timer | A change detection pass | None |
| Plain field mutated in a timer | Usually shows up | May not show up |
The last row is the main risk. Code that worked only because a zone triggered a pass will stop updating. The following chapters show how to find and fix that code.
When to wait
Consider keeping zones for now if:
- Your app depends on a third-party library that mutates state inside zone-patched callbacks and has no zoneless support.
- Many components use default change detection with plain mutable fields. These need conversion first.
- You cannot test the migration yet. Missed updates show up at runtime, not at compile time.
The migration can be incremental, which a later chapter covers.
Exercise
A component sets this.total = 5 inside a setTimeout callback. It is a plain field, not a signal, and the template reads {{ total }}. It updates in a zone-based app. Why might it stop updating in a zoneless app, and name one benefit you gain in exchange?
Show answer
In a zone-based app, Zone.js patches setTimeout and triggers change detection when the callback finishes, so the new value is rendered. In a zoneless app nothing notifies Angular that total changed, so the view stays stale unless total is a signal or markForCheck() is called. In exchange, unrelated events no longer trigger full change detection passes, and the zone.js polyfill can be removed from the bundle.