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 spot a long task. It also assumes you know that Zone.js patches browser APIs so Angular learns when async work finishes. Here you will find change detection in a recording, tell a cheap pass from an expensive one, and decide whether the cause is the pass itself or how often it runs.
What change detection looks like in a flame chart
Change detection is Angular's process of checking each component's bindings against their previous values and updating the DOM where they differ. In a trace it appears as a stack of function calls on the Main track, nested inside the task that triggered it.
Most traces follow this sequence:
- The browser starts a task, such as a click, a timer callback, or a resolved fetch.
- Zone.js frames wrap your callback. You will usually see names like
ZoneDelegate.invokeTask,Zone.runTask, orZoneTask.invokeTask. - Your code runs: the handler, the subscribe callback, or the timer function.
- When the zone has no more pending microtasks, Angular runs a change detection pass. You will see
ApplicationRef.tickor a similarly named frame. - Under that frame come calls such as
refreshView,detectChangesInViewIfRequired, andexecuteTemplate. These repeat once per component visited. - Finally Angular writes any changed values to the DOM. Style, layout, and paint work follow after the task ends.
Exact frame names change between Angular versions. Newer releases rename and reorganize internal functions. Do not memorize one name. Look for the shape: a wide Zone.js wrapper, your code, then a broad, repeating tree of view-refresh and template frames below it.
Source maps and minified names
In a production build, framework internals have short names like e.tick or Zu, and your own components may be minified too. Without source maps you will see one-letter functions and cannot tell which component owns the time. Serve the production build with source maps enabled, as the earlier chapter on production-like builds describes. In the Performance panel, source maps give you component names such as OrderListComponent_Template. These names are your main clue to which template is expensive.
Telling one pass from many
A trace can show change detection costing too much in two different ways. The fix differs for each.
| Pattern in the trace | What it means | Typical direction |
|---|---|---|
| One wide `tick`, tens of milliseconds or more | A single pass visits too much or does heavy work in templates | Find the expensive component or expression, then use OnPush and signals |
| Hundreds of narrow `tick` frames in a row | Something fires events at high frequency, such as scroll, mousemove, or a tight timer | Reduce the event rate or run the listener outside the zone |
| Periodic ticks with no user input | A `setInterval`, polling `Observable`, or `setTimeout` loop | Question whether the work needs to be inside the zone at all |
| Two similar passes back to back (development builds only) | Angular runs a second verification pass in dev mode | Ignore it. Profile a production build instead |
The last row is a common trap. In development mode, Angular runs a second check after each pass to catch bindings that changed during checking. This roughly doubles the work. If you profile ng serve output, you will see this extra pass and may chase a cost that does not exist in production.
Measuring the width of a pass
Click the tick frame in the flame chart and read the Summary tab at the bottom of the panel. Two numbers matter:
- Total time is the whole pass, including children.
- Self time is time spent in that function alone.
The tick frame itself usually has near-zero self time. The cost is in the children. Follow the widest child down at each level until you reach a frame that is wide and has high self time. That is where the work happens. If the widest leaf is a template function from one component, you have found a candidate. If the time spreads evenly across hundreds of small refreshView frames, the problem is breadth: too many components checked, so OnPush or list tracking is more likely to help than fixing one expression.
Use Bottom-Up to confirm. Select a time range covering the pass, open Bottom-Up, and sort by Self Time. Functions from your own code that rank high are where to look.
Counting passes with a small probe
The trace shows when passes happen. To see which component is being checked, add a temporary probe. The ngDoCheck hook runs each time Angular checks a component's inputs during a pass.
import { Component, DoCheck, Input } from '@angular/core';
@Component({
selector: 'app-row-probe',
template: `<span>{{ label }}</span>`,
})
export class RowProbeComponent implements DoCheck {
@Input() label = '';
private checks = 0;
ngDoCheck() {
this.checks++;
if (this.checks % 100 === 0) {
console.log(`RowProbe checked ${this.checks} times`);
}
}
}Place this on one row of a list. Interact with the page for ten seconds. If the count climbs by hundreds while you only moved the mouse, a high-frequency event is triggering passes. Remove the probe afterward, because it adds its own cost. For a per-component view without editing code, the Angular DevTools profiler covers this in the next chapter.
Matching a trace to a cause
Work through the trace in this order:
- Find the task. Click the long or repeated task on the Main track and look at its root frame. Is it a
click,scroll,setInterval,XMLHttpRequest, orPromisecallback? - Check how often it repeats. Zoom out and count similar tasks in a few seconds. Many small ones point to event frequency.
- Open the widest
tick. Follow the widest children to the template frame. - Check whether the expensive frame belongs to a template method call. Method calls in a template run on every pass, so a function that sorts or filters will show up in every
tick. - Look at the call counts. In Bottom-Up, a function that appears in hundreds of passes and is cheap each time is still a problem if the passes are too frequent.
Common mistakes when reading these traces
- Profiling
ng serve. Development mode adds the second verification pass and unminified, slower code. Compare against a production build before drawing conclusions. - Blaming Zone.js frames. The wide
ZoneDelegate.invokeTaskframes are wrappers. They look large because they contain everything beneath them. Their self time is small. - Recording with an idle page. If nothing in the app triggers work, there is nothing to see. Perform the slow interaction while recording and stop right after.
- Ignoring the rendering work after
tick. If the pass is short but the task is still long, look at the purple Recalculate Style and Layout frames. Those costs come from DOM changes that change detection wrote, not from the pass. - Reading one trace as a verdict. Single recordings vary. Record the same interaction three times before deciding which frame is the problem.
Exercise
You record a scroll interaction in a production build. The Main track shows about 300 short tasks in 5 seconds, each containing a narrow tick of 1 to 2 ms. No single task is long. Is the problem one expensive template, or the frequency of passes? What would you try first?
Show answer
The problem is frequency. Each pass is cheap, but scroll events fire so often that the passes add up and keep the main thread busy. Registering the scroll listener inside NgZone.runOutsideAngular stops each event from triggering a pass. Only re-enter the zone when the view really needs updating. Then record again and compare the number of tick frames.
Exercise
In a trace of ng serve, every click shows two nearly identical change detection passes in a row. Does this mean your component is being checked twice in production?
Show answer
No. In development mode Angular runs a second verification pass to detect bindings that change during checking. Production builds skip it. Record a production build to see the real cost.