Sub topic 7
Recording a profiler session
This chapter covers the Profiler tab of Angular DevTools: how to start a recording, what causes the data in it, and how to make a recording that you can compare later. It assumes you have the extension installed and can see your app in the Components tab. It also assumes you know what a change detection cycle is from the previous chapter, and that you can serve the app in development mode. Reading the results and fixing the problem come in the next two chapters.
What the profiler measures
The profiler records change detection. Each time Angular checks the component tree for changes and updates the DOM, the profiler logs the cycle. For every cycle it stores:
- The total time the cycle took.
- What triggered it, such as a click or a timer. DevTools calls this the source.
- For each component, how long its template function and its lifecycle hooks ran.
- For each directive, how long its lifecycle hooks ran.
It does not record network requests, layout, or painting. Use the browser's Performance panel for those. Angular DevTools answers a narrower question: which components cost the most time inside Angular's own work?
Prepare the app
A profile is only useful if the app is in the state you want to measure. Three things matter before you press record.
- Serve in development mode. Angular DevTools only attaches to apps built in development mode. A production build shows the "Angular application not detected" message. Use
ng servewithout--configuration production. - Reach the starting state. Navigate to the page, log in, and load the data first. Otherwise the recording includes startup work you did not mean to measure.
- Decide the action. Pick one interaction, such as clicking a button, typing in a field, or scrolling a list. Profiling "everything I do for a minute" yields a noisy chart that is hard to read.
Development mode adds extra checks, so absolute times are larger than in production. Use the numbers to compare components and compare before with after. Do not treat them as production timings.
A component worth profiling
The following example uses the slow component from this tutorial's plan and adds a button. The button changes nothing in the data. It exists to trigger a change detection cycle on demand.
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
@Component({
selector: 'app-report',
standalone: true,
template: `
<button (click)="tick()">Clicked {{ clicks }} times</button>
@for (row of rows; track row.id) {
<p>{{ row.name }}: {{ expensiveTotal(row) }}</p>
}
`,
})
export class ReportComponent {
clicks = 0;
rows = Array.from({ length: 500 }, (_, i) => ({
id: i,
name: `Row ${i}`,
values: [i, i + 1, i + 2],
}));
tick(): void {
this.clicks++;
}
// 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;
}
}
bootstrapApplication(ReportComponent);In a project created with the Angular CLI, put the component in its own file and keep the bootstrapApplication call in main.ts, as you normally would. The shape of the code is the same.
The loop in expensiveTotal runs 100,000 times per row, and the template calls the method for 500 rows. The component uses the default change detection strategy, so Angular re-runs the template on every cycle, even when only the click counter changed.
Record a session
- Open the browser's developer tools and select the Angular panel.
- Switch from the Components tab to the Profiler tab.
- Select the record button. The panel shows that a recording is in progress.
- Perform the single action you chose. In the example, click the button once.
- Select the button again to stop.
After you stop, DevTools draws a bar chart. Each bar is one change detection cycle, in the order they happened. The height and color of a bar show how long the cycle took relative to a frame budget. A cycle that takes longer than about 16 ms cannot finish inside one frame at 60 frames per second, so the page will feel less responsive. The panel's legend explains the exact color thresholds in your version, so check it instead of memorizing them.
Clicking the example button once should produce at least one tall bar. In practice you often see several bars for one click. Each source of change detection produces its own cycle, so a click handler, a timer, or a finished HTTP request each count separately.
What triggers a cycle
When you select a bar, DevTools shows its source. This tells you why Angular ran the check, which is often the first clue.
| Source you may see | Typical cause |
|---|---|
| A DOM event such as `click` or `input` | A user interaction handled by a template listener |
| A timer | `setTimeout` or `setInterval` callbacks that run inside the Angular zone |
| An HTTP or other async completion | A promise or observable that resolved |
| An animation or scroll event | Events that fire many times per second |
The exact labels vary between Angular versions. What matters is the pattern. If you recorded one click and see a long list of cycles from a timer you did not write, a library is probably scheduling work inside the Angular zone, and each timer tick triggers a full check.
Compare, do not trust one run
A single recording is a sample. The first cycle after page load is usually slower because the browser is still compiling code and warming caches. For reliable numbers:
- Record the same action at least three times and ignore the first run.
- Keep other tabs and heavy extensions out of the way.
- Take note of the order of magnitude, not the last digit. A drop from 120 ms to 15 ms is a finding. A drop from 120 ms to 118 ms is noise.
- Write down what the app state was, such as the number of rows. A profile with 50 rows and one with 500 rows cannot be compared.
DevTools can save a recording to a file and load it again later. Save your baseline before you change any code. After the fix you can record again and compare the two files side by side, which makes the result of the OnPush chapter measurable and not a guess.
Common mistakes
- Recording for too long. A long session fills the chart with hundreds of bars. Record the single action, then stop.
- Profiling in a production build. DevTools reports that no Angular app was found, or the Profiler stays empty. Rebuild in development mode.
- Forgetting the zone. Work that runs outside the Angular zone, for example inside
NgZone.runOutsideAngular, triggers no cycle and does not appear. If an interaction feels slow but no bar is tall, the cost may lie outside Angular, and you should switch to the browser's Performance panel. - Blaming the first bar. Page load work, such as creating components, shows up as an early tall bar. It is a different problem from slow updates later.
- Changing the app while recording. If you also open panels or select components in the Components tab during the recording, you may add extra cycles.
Checking your recording
After you stop, run through these checks before moving on.
- The chart contains at least one bar for the action you performed.
- Selecting a bar shows a source that matches the action, such as a click.
- The component breakdown lists
ReportComponentwith a time much larger than any other entry. In the example, almost all of the cycle time belongs to this one component becauseexpensiveTotalruns inside its template. - You have saved the recording or noted its total time as the baseline.
If check 3 fails and no component stands out, either the cost is outside Angular or your action did not touch the slow part of the tree.
Exercise
You record a profile of a production build served with ng serve --configuration production and the Profiler tab shows no Angular application. What do you change, and why?
Show answer
Serve the app in development mode, for example with a plain ng serve. Angular DevTools relies on debugging information that Angular only exposes in development mode, so a production build is not detected and cannot be profiled.
Exercise
A recording of one button click shows five bars. Two of them have a timer as their source, and you did not write any timer code. What should you suspect, and what would you check next?
Show answer
Suspect a library or service that schedules work with setTimeout or setInterval inside the Angular zone, which triggers a full change detection cycle on each tick. Select each timer bar to see which components took time, search the code and its dependencies for timers, and consider running the timer outside the zone with NgZone.runOutsideAngular if the work does not need to update the view.
Exercise
Why should you record the same action several times and discard the first recording before you compare two versions of your code?
Show answer
The first cycle often includes one-time costs such as code compilation and cache warm-up, so it is slower than later cycles. Comparing a cold run to a warm run would make a code change look better or worse than it really is. Repeated recordings of the same action under the same state give comparable numbers.