Sub topic 8
Reading change detection results
This chapter assumes you can already open the Angular DevTools panel and record a profiler session, which the previous chapter covered. It also assumes you know the basics of change detection: Angular walks the component tree and re-evaluates template bindings after events, timers, and HTTP responses. Here you learn to read what a recording shows and decide which component to fix first.
What a recording contains
A profiler recording is a list of change detection cycles. A cycle is one pass where Angular checks the component tree for changes and updates the DOM. The profiler shows two views of that data:
- A bar chart across the top, with one bar per cycle in the order the cycles happened.
- A detail view below it for the bar you select, shown as a flame graph or a ranked list.
The bar chart tells you which cycles were slow. The detail view tells you which components made them slow. Read them in that order.
Reading the bar chart
Each bar is one cycle. Its height and color indicate how long the cycle took, and hovering over a bar shows the duration in milliseconds. The color is tied to the frame budget. At 60 frames per second a frame lasts about 16 ms, and the browser must also do layout and paint inside that time. A cycle that uses most of the budget leaves no room for the rest, and the user sees dropped frames.
Look for these patterns:
| Pattern in the bar chart | Likely meaning |
|---|---|
| A few tall bars among many short ones | One-off expensive work, such as the first render or a large data load |
| Every bar equally tall | Each cycle re-runs the same costly work, such as a method called from a template |
| Many bars while you did nothing | Something keeps triggering cycles, such as a timer, a polling call, or a mouse-move listener |
| One bar per keystroke, each tall | Input handlers cause a full-tree check on every key |
When you select a bar, DevTools may also show the source that triggered the cycle, for example a click listener or a timer callback. Use this field to separate "this cycle is slow" from "too many cycles are happening". Those two problems have different fixes.
Reading the flame graph
The flame graph shows how a single cycle was spent. Each box is a component or directive, and its width is proportional to the time spent in it. Boxes are stacked to mirror the component tree: a parent sits below its children, so the root is at the bottom.
Two things matter when you read it:
- Wide boxes. A wide box is where time went. Find the widest box, then check whether its children account for the width. If the children are narrow and the parent is wide, the cost is in the parent's own template and bindings.
- Boxes that appear in every cycle. If the same component is wide in every selected bar, it is a stable cost, and a good candidate for a fix.
A box shows its time and, on hover, the directive's name and how much time went to its own template versus its children. Self time is the number to chase. A parent with a large total but small self time is only a container for an expensive child.
The ranked view lists the same data as a sorted list. Use it when the tree is deep and widths are hard to compare, because it puts the most expensive entries at the top.
Applying it to the slow component
Take the component from the code example with the costly template method:
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;
}
}Work out what to expect before you record. The template calls expensiveTotal once per row, and each call loops 100,000 times. With 500 rows, that is 50 million loop iterations in every cycle. If you record while clicking around the page, you should see:
- Tall bars in the bar chart, roughly equal height, because every cycle repeats the same work.
- In the flame graph for any bar,
ReportComponentis the widest box by far, and its children are narrow. - In the ranked view,
ReportComponentis at the top with most of the time as self time.
That combination, equal bars plus one wide self-time box, points to work done in the template of one component on every pass. The cause is the method call in the binding. Angular cannot know whether expensiveTotal would return a new value, so it calls it every time.
Separating two problems
Slow cycles and frequent cycles look different in the recording, and you need to name which one you have before changing code.
- Slow cycles. Few bars, each tall. Look in the flame graph for the wide self-time box and fix that component's template work.
- Frequent cycles. Many short bars, possibly with no user action. Check the trigger shown for each bar. A
setIntervalor a scroll listener inside a component that fires many times per second makes Angular run a full check each time. Move that code out of the Angular zone, or reduce how often it fires.
flowchart TD
A[Record a session] --> B{Many bars with no user action?}
B -- Yes --> C[Check the trigger source]
C --> D[Reduce or move out the timer or listener]
B -- No --> E{Bars tall?}
E -- No --> F[Cycle cost is acceptable]
E -- Yes --> G[Select a tall bar and open the flame graph]
G --> H[Find the widest self-time box]
H --> I[Fix that component's template work]
I --> J[Record again and compare]The diagram above is the reading procedure as a decision path. Notice that it ends with a second recording. A fix that you have not measured is a guess.
Gotchas when reading results
Development mode inflates numbers
Angular DevTools only works with an app in development mode, and development mode does extra work. Notably, after each pass Angular runs a second pass to confirm no binding changed during the check. The absolute milliseconds you see are higher than production would produce. Use the numbers to compare before and after on the same machine, and do not quote them as production performance.
Template method calls run more often than you expect
Because of that second pass, a method called from a template runs at least twice per cycle in development mode. If you add a console.log counter to expensiveTotal to confirm how often it runs, expect double the production count. The profiler is the better tool here, because the counter's value depends on the mode.
The first cycle is usually the most expensive
The first bar after you start recording, or after navigating, often includes creating components and building the DOM. That is a different cost from steady-state checking. If you only care about repeated checks, record again after the page has settled and then perform the action you are testing.
Record one action at a time
If you click five buttons during a recording, the bar chart is a mix of five causes. Start the recording, perform one interaction, and stop. Then each bar in the result maps to that interaction, and you can tell which bar caused the problem.
A wide parent is not always the culprit
A box that is wide because its children are wide is not the problem. Compare the box's own time to the time of the boxes above it in the stack. Fix the box where self time is high.
Small bars can hide a large total
One 3 ms cycle is cheap. Two hundred of them in a second is 600 ms of work. If the bar chart is crowded, judge the total number of cycles as well as the height of any one bar.
Writing down your finding
Before you change code, write one sentence that names the component, the cost, and the cause. For the example above it would be: "ReportComponent spends most of every cycle in its own template, because expensiveTotal is called for 500 rows on every pass." This sentence tells you what the fix has to achieve, namely that the total is computed only when the data changes. The next chapter applies OnPush and a computed signal to do exactly that, and then compares a new recording to the one you took here.
Exercises
Exercise
A recording shows 40 short bars in five seconds, although you did not touch the page. The flame graph for each bar shows a small cost. Is the problem slow cycles or frequent cycles, and where do you look next?
Show answer
Frequent cycles. Select a bar and check the trigger shown for the cycle, such as a timer or listener. Then find the code that fires it, and reduce its frequency or run it outside the Angular zone.
Exercise
In the flame graph, ParentComponent is the widest box, but the boxes for its children together fill almost all of that width. Which component do you investigate?
Show answer
Investigate the children. The parent's width is mostly its children's time, so its own self time is small. Find the child with the largest self time and fix its template work.
Exercise
You add a console.log inside expensiveTotal and it prints twice as many times as there are rows after one change. Does this mean Angular ran two real cycles for the action?
Show answer
No. In development mode Angular runs a second pass after each check to verify that no binding changed, so template method calls run twice. The count would be lower in production. Use the profiler, not the counter, to measure cost.