Sub topic 8
Reading change detection results
This chapter assumes you can run an Angular app in development mode, have Angular DevTools installed, and know what change detection does. The previous chapter covered how to record a session. Here you learn to read what the recording shows.
What a recording contains
A recording is a series of bars in the Profiler tab. Each bar is one change detection cycle, meaning one pass in which Angular checked your components and updated the DOM where values had changed. The bar's height and color show how long the cycle took. Taller bars with warmer colors are slower.
Selecting a bar opens a breakdown for that cycle. You can view it in two ways:
- Bar chart: Each component that was checked appears as a bar. Its time is the time spent in that component's own checks, not in its children.
- Flame graph: The component tree is drawn as stacked rows, with each component's children beneath it. A wide block means that component and its subtree took a long time.
Use the bar chart to find the single most expensive component. Use the flame graph to see whether the cost sits in one component or is spread across a subtree.
Which numbers to look at
For each selected cycle, check three things:
- Total time of the cycle. A cycle that takes longer than one frame (about 16 ms at 60 frames per second) can make the page feel sluggish.
- Time per component. Look for one component that accounts for most of the cycle.
- Number of cycles. If many cycles run while nothing visibly changes, something is triggering change detection too often, such as a timer, an event listener, or a polling loop.
You can also filter the recording to hide cycles below a time threshold, so the slow ones stand out.
Reading the slow example
Consider the report component below. It uses default change detection and calls expensiveTotal from its template.
import { Component } from '@angular/core';
@Component({
selector: 'app-report',
standalone: true,
template: `
@for (row of rows; track row.id) {
<p>{{ row.name }}: {{ expensiveTotal(row) }}</p>
}
`,
})
export class ReportComponent {
rows = Array.from({ length: 500 }, (_, i) => ({ id: i, name: `Row ${i}`, values: [i, i + 1, i + 2] }));
// Called for every row on every change detection pass.
expensiveTotal(row: { values: number[] }): number {
let total = 0;
for (let n = 0; n < 100000; n++) {
total += row.values[n % row.values.length];
}
return total;
}
}In a recording, you would expect to see this pattern:
- Every cycle is long, even when you only click an unrelated button elsewhere in the app.
ReportComponentdominates the bar chart.- In the flame graph, its block is wide, and its children add little.
The method runs 500 times per cycle, once per row, with 100,000 loop iterations each. Angular must call it again on every pass because it cannot know whether the result changed. That is why unrelated events make the cycles slow.
Common reading errors
- Blaming the wrong component. A parent can look slow because its template contains costly children. Check whether the time is the component's own or its subtree's.
- Profiling a single cycle. One slow bar may be a first render or a one-time load. Look at several cycles and compare.
- Comparing unlike recordings. Record the same user action each time. Otherwise a difference in the numbers may come from the action and not from your change.
- Expecting zero work from a skipped component. Components that Angular did not check do not appear. Their absence is the evidence that an optimization worked.
Saving a baseline
Before changing code, note the total cycle time and the top component's time for a fixed action, such as clicking one button three times. After the fix, repeat the same action and compare. The next chapter uses this comparison to confirm that OnPush reduced the cost.
Exercise
A recording shows 40 cycles in ten seconds while the page is idle, and ReportComponent is the widest block in each one. Which two separate problems does this suggest, and how do you tell them apart in the Profiler?
Show answer
One problem is that something triggers change detection too often when nothing changes, which you see as a high number of cycles with no visible change. The other is that ReportComponent's checks are expensive, which you see as it dominating the bar chart and flame graph in each cycle. Fixing the trigger reduces the number of cycles. Fixing the component reduces the time per cycle. They are independent, so compare both the cycle count and the per-cycle time before and after each change.
Exercise
Why does the bar chart show a component's own time while the flame graph shows its subtree, and when would you prefer the flame graph?
Show answer
The bar chart attributes time to the component itself, so it finds the single most expensive component. The flame graph nests children under parents, so a wide block shows the total cost of a component and everything below it. Prefer the flame graph when a parent looks slow and you need to know whether the cost is in the parent or in its children.