Sub topic 14
Finding unnecessary checks with Angular DevTools
This chapter assumes you know the Default and OnPush strategies, how Zone.js triggers a change detection cycle, and how signals work in templates. Here you will measure which components Angular checks, so you can decide where OnPush or a different trigger would help.
What Angular DevTools shows
Angular DevTools is a browser extension for Chrome and Firefox. It adds an Angular tab to the browser's developer tools. It works with a running development build of your app. Two of its panels matter here:
- Components shows the component tree with each component's inputs and state.
- Profiler records change detection cycles and reports how long each component took.
The profiling workflow
Measure before you change anything. A component that is checked often is only a problem if its check is slow or if the cycles happen at a bad moment.
- Open the Profiler tab and press the record button.
- Perform the action you suspect is wasteful, such as typing in a search box or moving the mouse.
- Stop recording. The bar chart shows one bar per change detection cycle. Taller bars took longer.
- Select a bar. The panel lists the components checked in that cycle and the time spent in each one.
Many bars during an interaction that should change nothing on screen suggest that an event source is triggering cycles needlessly. A few tall bars suggest that a particular component's check is expensive.
A component worth profiling
The component below runs a sorting function from its template. Under the Default strategy, Angular calls that function on every cycle, even when items has not changed.
import { Component } from '@angular/core';
@Component({
selector: 'app-sorted-list',
template: `
<input (input)="query = $any($event.target).value" />
<ul>
@for (item of sorted(); track item) {
<li>{{ item }}</li>
}
</ul>
`,
})
export class SortedList {
items = ['pear', 'apple', 'fig', 'banana'];
query = '';
sorted() {
console.log('sorting');
return [...this.items].sort();
}
}Record while typing in the input. The profile shows one cycle per keystroke, and the component's check time is repeated each time. The console.log call confirms that sorted() runs on every keystroke.
Applying the fix and comparing
Move the work into a computed signal and switch to OnPush. The sort now runs only when items changes.
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-sorted-list',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<input (input)="query.set($any($event.target).value)" />
<ul>
@for (item of sorted(); track item) {
<li>{{ item }}</li>
}
</ul>
`,
})
export class SortedList {
items = signal(['pear', 'apple', 'fig', 'banana']);
query = signal('');
sorted = computed(() => {
console.log('sorting');
return [...this.items()].sort();
});
}Record the same interaction again. Typing still triggers a cycle, because the input's event listener fires. The difference is that sorting is no longer logged on each keystroke, and the component's time in the profile drops. Compare the two recordings side by side rather than trusting your impression.
Reading the results with care
- Profile a development build. Development mode adds checks that production does not, so absolute times are inflated. Use the relative differences.
- A cycle is not a render. A cycle that checks a component without finding a changed binding is cheap. Cost comes from slow expressions, large template trees and many bindings.
- Event sources matter. If cycles fire with no user action, look for timers, polling or listeners such as
mousemove. Those are candidates forrunOutsideAngular.
Summary of the symptoms
| What you see in the Profiler | Likely cause | First fix to try |
|---|---|---|
| Many cycles during idle mouse movement | Global listener inside the zone | `runOutsideAngular` |
| One component dominates each cycle | Expensive template expression | `computed` signal or a pure pipe |
| Whole subtree checked on every event | Default strategy on stable components | OnPush |
Exercise
You record a profile while the user moves the mouse over a map component, and you see hundreds of short cycles with no visible change on screen. Which change would you try first, and how would you check that it worked?
Show answer
Register the mousemove listener inside NgZone.runOutsideAngular so it stops triggering change detection, and re-enter the zone with NgZone.run only when the view needs an update. Then record the same interaction again and confirm that the number of bars in the Profiler chart has dropped.