Sub topic 9
Profiling components with Angular DevTools
The Performance panel shows that change detection ran. Angular DevTools shows which components it ran in and how long each one took. This chapter assumes you can read a trace in the Performance panel and know what change detection does, including the effect of OnPush.
Setup and a limitation
Angular DevTools is a Chrome extension. After you install it, an Angular tab appears in DevTools on pages that run Angular. The extension reads data from Angular's development-mode hooks, so the page must be a development build served with ng serve. A production build shows "Angular application not detected" or an equivalent message.
Use it to find which components are expensive. Then confirm the absolute timings in a production-like trace.
Recording a profile
- Open the Angular tab and choose Profiler.
- Click Record.
- Perform the interaction you want to study, such as typing in a search box.
- Click Stop.
Reading the results
The profiler shows a bar chart. Each bar is one change detection cycle, and a taller bar means the cycle took longer. Colors mark the slow cycles. A cycle above roughly 16 ms cannot fit in a 60 fps frame.
Select a bar to open the flame graph for that cycle. It shows the component tree. Each box is a component, and its width is the time spent on it. Hover over a box to see the time spent in its template and in its lifecycle hooks. A switch lets you view the same cycle as a ranked list, sorted by time.
Watch for two patterns:
- One wide component. Its template, a lifecycle hook, or a getter does costly work. The next chapters cover template costs.
- Many cycles, each cheap. Something triggers change detection too often. A timer, a scroll listener, or a polling call is a likely cause.
A typical culprit
This component runs a calculation every time Angular checks it:
import { Component } from '@angular/core';
@Component({
selector: 'app-report',
template: `<p>Score: {{ score() }}</p>`,
})
export class ReportComponent {
data = Array.from({ length: 50_000 }, (_, i) => i);
score() {
return this.data.reduce((sum, n) => sum + Math.sqrt(n), 0);
}
}In the profiler, ReportComponent appears in every cycle with the same cost, even when nothing about it has changed. That repeated cost is the signal to look for.
Common mistakes
- Profiling with the browser console open and many
console.logcalls. The logging skews the numbers. - Treating development-mode timings as final. Development mode adds extra checks, so use the profiler for relative comparisons.
- Recording a long session. Record one interaction at a time so the bars stay readable.
Exercise
You record a profile while typing in a search box. The chart shows 40 short bars, and each one is 3 ms. Is the problem a single slow component or too many cycles? Which fix would you try first?
Show answer
The problem is too many cycles. No single cycle is slow, but 40 cycles of 3 ms add up to 120 ms of work. Look for what triggers each cycle, such as a keystroke handler or a timer. Then reduce the triggers by debouncing the input or running the handler outside the Angular zone. Cutting the cost of each cycle would help less.