Sub topic 7
How Zone.js triggers Angular work
This chapter assumes you can read a flame chart (see the previous chapters) and know what a task on the browser event loop is. It explains why Angular's change detection usually appears right after your own code in a trace.
What Zone.js does
Angular does not watch your data. Zone.js tells it when to look. Zone.js is a library that patches browser asynchronous APIs, such as setTimeout, Promise, addEventListener, and XMLHttpRequest. When your code schedules a callback, Zone.js wraps it. When the callback finishes, Zone.js reports that to Angular's NgZone.
NgZone then emits onMicrotaskEmpty once no more microtasks are queued. ApplicationRef listens for that signal and runs tick(), which is a change detection pass over the component tree.
Observing it in code
This component logs each time Angular is about to check views. Click the button and the timer callback triggers a pass, even though you never call Angular directly.
import { Component, NgZone, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@Component({
selector: 'app-zone-demo',
template: `
<button (click)="start()">Start</button>
<p>Count: {{ count }}</p>
`,
})
export class ZoneDemoComponent {
private zone = inject(NgZone);
count = 0;
constructor() {
this.zone.onMicrotaskEmpty
.pipe(takeUntilDestroyed())
.subscribe(() => console.log('Angular is about to run change detection'));
}
start() {
setTimeout(() => {
this.count++;
}, 500);
}
}The click triggers one pass. Half a second later, the setTimeout callback triggers another.
What this looks like in a trace
In the flame chart, a zone-triggered pass has a recognizable shape:
- A task starts with an event or timer entry, such as
Event: clickorTimer Fired. - Zone.js frames appear below it. Names such as
ZoneDelegate.invokeTaskoronInvokeTaskvary by version. - Your handler runs.
- After your handler returns,
ApplicationRef.tick(or a similarly named frame) runs and calls into your components' templates.
If the tick frame is wide, the cost is in change detection, not in your handler. If many short tasks each end in a tick, something is firing frequently inside the zone.
Common errors
- Assuming only your handler matters. Every patched async callback causes a pass, including ones that change nothing visible, such as polling timers and
mousemovelisteners. - Blaming Angular for third-party work. A chart library that schedules timers inside the zone triggers a full pass for every timer.
- Expecting a pass without the zone. Code run through
runOutsideAngular, or an app without Zone.js, will not update the view automatically. Later chapters cover when that is the right choice.
Exercise
In a trace, you see 60 short tasks per second, each ending in a wide ApplicationRef.tick frame, and your own handler code is tiny. What does this suggest, and what would you inspect next?
Show answer
Something inside the Angular zone is firing frequently, such as a timer, a scroll or mousemove listener, or a library callback. Each firing triggers a full change detection pass. Inspect which event or timer entry starts each task, then check whether that work can run outside the zone or whether the passes can be made cheaper with OnPush and signals.