Sub topic 9
Profiling components with Angular DevTools
The Performance panel shows where the main thread spent its time, but it labels work with function names from your bundle. Angular DevTools adds a component-level view. It reports which components ran change detection, how often, and how long each took. This chapter covers how to record with the Profiler, how to read its charts, and how to turn a finding into a fix.
This chapter assumes you can open Chrome DevTools and record a trace in the Performance panel. It also assumes you know what change detection is and what OnPush does.
What Angular DevTools adds
Angular DevTools is a browser extension for Chrome. After you install it, an Angular tab appears in DevTools. It has two main parts:
- Components shows the component tree, with inputs and state you can inspect.
- Profiler records change detection cycles and attributes time to components.
The Performance panel answers "which function was slow?" The Profiler answers "which component was checked, and why was it checked so often?" Use both. The Profiler points you at a suspect component, and the Performance panel confirms the cost in a realistic build.
Development mode adds extra checks. Angular runs a second pass after each change detection cycle to catch expression changes that happened during the first pass. That makes every cycle slower than it would be in production. Compare Profiler results with other Profiler results, and confirm any fix with a production-like trace.
Recording a profile
- Run the app with
ng serveand open it in Chrome. - Open DevTools and select the Angular tab, then Profiler.
- Click Record.
- Perform one interaction, such as typing in a search box or scrolling a list.
- Click Stop.
Keep the recording short and focused on a single interaction. A 30-second recording of general clicking produces hundreds of bars and no clear conclusion. Aim for an interaction you can repeat exactly, because you will need to repeat it to verify a fix.
Reading the bar chart
After you stop, the Profiler shows a bar chart. Each bar is one change detection cycle. Two things matter:
- Height and color. A taller or warmer-colored bar means the cycle took longer. Hover over a bar to see the exact duration.
- Count. Many bars from one interaction mean change detection ran many times. Each scroll event or timer tick produces a cycle, so a high-frequency source shows up as a dense row of bars.
Two kinds of problem show up here, and they need different fixes:
| Pattern in the chart | Meaning | Typical fix |
|---|---|---|
| A few very tall bars | Each cycle does too much work | Cheaper templates, `OnPush`, signals |
| Many short bars | Cycles run too often | Run the event source outside the zone, debounce |
| Many tall bars | Both problems at once | Fix the frequency first, then the per-cycle cost |
Select a bar to see its details. Recent versions of the extension also show what triggered the cycle, such as a click listener or a timer. This connects back to the Zone.js chapter. Zone.js notified Angular that something might have changed, and this label tells you which task it was.
Reading the flame graph
When you select a bar, the Profiler shows that cycle as a flame graph. Each block is a component. The block's width is the time spent checking it, and children appear below their parent.
The Profiler offers three views of the same cycle:
| View | Best for |
|---|---|
| Flame graph | Seeing where a component sits in the tree and which parent contains the cost |
| Ranked | Finding the single slowest component in a cycle |
| Hierarchical | Following the cost down through nested components |
Start with the ranked view to find the top entry. Then switch to the flame graph to see its parents and children. Hover over a block to see how long the component spent in its template function and in lifecycle hooks such as ngDoCheck.
Read the flame graph with these points in mind:
- Wide blocks are expensive. The time includes the template function and the component's own hooks, so a wide block can mean heavy template expressions or slow
ngDoCheck. - A component checked in every cycle is a candidate for
OnPush. If a leaf component shows up in every bar but its inputs never change, it is being checked for nothing. - A grayed or absent block means Angular skipped it. That is what you want to see after adding
OnPush.
A worked example
Suppose an order summary component calls a method from its template:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-order-total',
template: `<p>Total: {{ total() }}</p>`,
})
export class OrderTotalComponent {
@Input() items: { price: number; qty: number }[] = [];
total() {
return this.items.reduce((sum, i) => sum + i.price * i.qty, 0);
}
}This component uses the default change detection strategy, so Angular calls total() in every cycle, even when items has not changed. With a large order and a page that triggers many cycles, the Profiler shows OrderTotalComponent as a wide block in every bar.
The fix uses OnPush and a computed signal, so the total is recalculated only when the input changes:
import { ChangeDetectionStrategy, Component, computed, input } from '@angular/core';
@Component({
selector: 'app-order-total',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Total: {{ total() }}</p>`,
})
export class OrderTotalComponent {
items = input.required<{ price: number; qty: number }[]>();
total = computed(() =>
this.items().reduce((sum, i) => sum + i.price * i.qty, 0)
);
}Record the same interaction again. The block for this component should shrink or disappear from most cycles. The component is now checked only when its input changes. Note that total() in the template is still a function call. The difference is that it reads a cached signal value, not a calculation.
Common mistakes
- Trusting absolute times. Development-mode timings are inflated. Use them to rank components and to compare before and after, not to claim a cycle takes a specific number of milliseconds in production.
- Recording with other extensions active. Other extensions can add their own work to the page. Profile in an incognito window with only Angular DevTools enabled, or in a clean Chrome profile.
- Skipping CPU throttling. On a fast laptop, a slow component may look harmless. Apply CPU throttling in the Performance panel's settings before recording so differences stand out.
- Optimizing the widest block without checking frequency. A component that takes 2 ms but runs in 200 cycles costs more than one that takes 20 ms in one cycle. Look at the bar count as well as the width.
- Changing several things at once. If you add
OnPush, change a list's tracking, and move a listener outside the zone in one commit, you cannot tell which change helped. Change one thing per recording.
When the Profiler is not enough
The Profiler measures change detection only. It does not show network time, layout, paint, or garbage collection. If the bars look healthy but the page still feels slow, go back to the Performance panel and look for long tasks that are not change detection, such as layout work or a heavy third-party script. The Profiler narrows down Angular's share of the problem. The flame chart in the Performance panel shows the rest.
Exercise
You record a profile of a scroll interaction in Angular DevTools. The bar chart shows about 300 short, low bars, and the ranked view shows no single component above 1 ms. Which problem is this, and what is the first fix to try?
Show answer
This is a frequency problem, not a per-cycle cost problem. Each scroll event triggers a change detection cycle through Zone.js. The first fix is to register the scroll listener inside NgZone.runOutsideAngular so that scroll events no longer trigger cycles, and to update the view deliberately only when something actually needs to change. Then record the same scroll again and check that the bar count drops.
Exercise
Why can you not use Angular DevTools to profile a production build, and how should you confirm that a fix found with the Profiler works in production?
Show answer
Angular DevTools relies on hooks that exist only in development mode, so it does not detect a production build. To confirm a fix, record the same interaction in the Performance panel against a production-like build with source maps and the same CPU throttling, and compare the before and after traces.