Sub topic 7
Recording a profiler session
This chapter assumes you know what change detection is (covered in the previous chapter), can read basic TypeScript, and have an Angular app running in development mode with Angular DevTools installed. You will record a profile of a slow component and read the first level of results.
What the profiler records
The Profiler tab in Angular DevTools records change detection cycles. A cycle is one pass in which Angular checks components and updates the DOM. For each cycle, the profiler stores how long it took and which components and directives ran work during it.
Recording is manual. Nothing is captured until you start it, so you control which interaction ends up in the data.
A component worth profiling
The following component uses the default change detection strategy. Its template calls expensiveTotal for every row, so each cycle runs that method 500 times.
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;
}
}Each call loops 100,000 times, so one cycle does about 50 million iterations. Any event that triggers change detection, such as a click or a timer, pays this cost again.
Steps to record
- Serve the app in development mode and open it in a supported browser.
- Open the browser developer tools and select the Angular tab, then the Profiler sub-tab.
- Click the record button (a circle) to start.
- Interact with the app. Do the one action you want to measure, such as clicking a button or typing in a field.
- Click the button again to stop.
Keep the recording short and focused. A profile of one click is easier to read than a profile of a minute of random use.
What you see after stopping
After you stop, the profiler shows a bar chart with one bar per change detection cycle, in order. Bar height reflects the time spent in that cycle, and color indicates how that time compares with the frame budget. Taller, warmer bars are the cycles to investigate.
Selecting a bar shows the components that were checked in that cycle and the time each one took. The detailed reading of these views is the subject of the next chapter. For now, confirm the basics:
- Your interaction produced at least one bar.
- The bar for the cycle that ran
expensiveTotalis clearly taller than the others. - The component list for that cycle includes
ReportComponentwith most of the time attributed to it.
Common problems
| Symptom | Likely cause | What to do |
|---|---|---|
| The Angular tab does not appear | The page is not detected as Angular, or the app is a production build | Serve in development mode and reload the page |
| The profile has no bars | No change detection ran while recording | Repeat the interaction that updates the view |
| Many tiny bars and no clear culprit | Timers or polling trigger frequent cycles | Record a single interaction, or pause background work |
| Timings change from run to run | Browser extensions, other tabs, or a cold cache | Record the same action two or three times and compare |
Summary of the technique
Record one interaction, stop, then look for the tallest bars. Save a note of the cycle time for the interaction you measured, because you will compare against it after the OnPush change in a later chapter.
Exercise
You click a button once in your app and want to know which component slows the update. Describe how you would record the profile so the result is easy to read, and what you should do before changing any code.
Show answer
Open the Profiler tab, start recording, perform only that one click, and stop recording. This gives a short profile with few cycles. Before changing any code, keep the baseline result (the cycle times and the slowest components) so a new profile after the fix can be compared against it.
Exercise
A profile of your app shows no bars at all. Name two possible causes.
Show answer
The app may be a production build or not detected as an Angular app in development mode, or no change detection ran during the recording because the interaction did not update the view.