Sub topic 8
Recognizing change detection in a trace
This chapter assumes you can record a trace in the Performance panel, read a flame chart, and know that Zone.js notifies Angular after async work. You will learn which flame chart entries are change detection and what their shape tells you.
What change detection looks like
Change detection is the work Angular does to check bindings and update the DOM. In a trace it appears on the Main track as a tree of function calls that starts after an event or timer task. Exact names vary by Angular version, but these are common:
ApplicationRef.tick: the root of one change detection pass.refreshViewordetectChangesInView: Angular checking one component view. Nested calls mean nested components.executeTemplate: your template running. Time directly under it is spent in binding expressions.ZoneDelegate.invokeTaskorZone.runTask: Zone.js running the callback that caused the pass.
Search the trace with Ctrl+F (Cmd+F on macOS) for tick to jump between passes.
Patterns to look for
| What you see | What it suggests |
|---|---|
| A `tick` after every mousemove, scroll, or timer | Frequent events are inside the Angular zone |
| Many `refreshView` calls under one `tick` | Many components are checked, so they may lack OnPush |
| A wide `executeTemplate` with a single function under it | A costly template expression |
| Two passes per event in development | The extra `checkNoChanges` pass, which does not run in production builds |
| `tick` runs close to a timer's interval | A `setInterval` or polling loop is triggering checks |
A worked example
This template calls a method on every check:
import { Component } from '@angular/core';
@Component({
selector: 'app-report',
template: `<p>{{ summary() }}</p>`,
})
export class ReportComponent {
data = Array.from({ length: 50000 }, (_, i) => i);
summary() {
return this.data.reduce((a, b) => a + b, 0);
}
}Record a trace while moving the mouse over the page. Each event produces a tick, and under it summary appears inside executeTemplate. The repeated, identical blocks show that the work reruns even though data never changed. If you see this shape, go to the Angular DevTools profiler to find which component is responsible.
Exercise
A trace shows 60 tasks per second. Each task is a mousemove handler followed by ApplicationRef.tick, and the handler only updates a non-visual counter. What does this pattern suggest, and what is one fix?
Show answer
Each high-frequency event triggers a full change detection pass through Zone.js, even though nothing in the view changes. One fix is to register the listener inside NgZone.runOutsideAngular, so the events no longer trigger tick. Using OnPush components to shrink each pass would also reduce the cost.