Sub topic 6
Change detection: the background you need for profiling
This chapter assumes you know Angular components and templates, and that you have the DevTools extension installed from the earlier chapters. It explains what Angular does on each change detection cycle, so the profiler's numbers make sense when you record a session in the next chapter.
What change detection does
Change detection is how Angular keeps the DOM in step with your component data. Angular walks the component tree, re-evaluates each template's bindings, and updates the DOM wherever a value differs from the last check.
A cycle is one full pass of this work. The profiler reports each cycle as a bar, so a long bar means a slow pass.
What starts a cycle
Angular doesn't poll your data. Something has to tell it that state may have changed. In an app that uses zone.js, which is the default setup, zone.js patches browser APIs and notifies Angular after:
- DOM events such as
clickandinput - timers (
setTimeout,setInterval) - finished HTTP requests and resolved promises
Signal updates read by a template also schedule a refresh. Calling ChangeDetectorRef.markForCheck() marks a component to be checked on the next cycle.
Default strategy: check everything
With the default strategy, every component is checked on every cycle, whether or not its own data changed. A click in one corner of the page re-checks the whole tree.
Each check re-evaluates the template expressions. A method call such as {{ label() }} therefore runs on every cycle. The following component shows this:
import { Component } from '@angular/core';
@Component({
selector: 'app-demo',
standalone: true,
template: `
<button (click)="clicks = clicks + 1">Clicked {{ clicks }}</button>
<p>{{ label() }}</p>
`,
})
export class DemoComponent {
clicks = 0;
calls = 0;
label(): string {
this.calls++;
console.log('label() calls:', this.calls);
return 'Static text';
}
}The text never changes, yet the console logs a new label() calls: line after each click. Angular can't know the method returns the same value, so it calls it every time. If that method were expensive, every cycle would pay for it.
OnPush: check only when there is a reason
ChangeDetectionStrategy.OnPush tells Angular to skip a component unless one of these happens:
- An
@Inputreceives a new reference. Mutating an object or array in place doesn't count. - An event handler in the component's template, or in a child's template, runs.
- A signal the template reads changes.
- The
asyncpipe receives a new value, or code callsmarkForCheck().
When a component is skipped, Angular also skips its children, unless they are marked for checking for one of the reasons above. Skipped components cost almost nothing, which is why OnPush is the usual fix for slow cycles. A later chapter applies it and verifies the result in a new profile.
Costs you will see in the profiler
The time in one cycle comes from three places:
- Template expressions: method calls and getters run on every check.
- Lifecycle hooks:
ngDoCheckandngAfterViewCheckedrun on every cycle, even for OnPush components. - Component count: a larger tree means more checks per cycle.
The profiler attributes time to individual components. That tells you where to look, but you still have to decide whether the cause is an expensive expression, a hook, or just too many components being checked.
The development-mode double check
In development mode, Angular runs a second pass after each cycle to confirm that no binding changed during the first. If one did, it throws ExpressionChangedAfterItHasBeenCheckedError. This catches bugs where a child changes state that a parent has already rendered.
The second pass also means development cycles do more work than production ones. Compare profiles taken in the same mode, and use the relative differences between components rather than the absolute milliseconds.
Common mistakes
- Mutating an array or object in place under an OnPush parent. The reference stays the same, so the view doesn't update.
- Putting heavy work in a template method or getter and assuming Angular caches the result. It doesn't.
- Assuming OnPush stops all work. Lifecycle hooks such as
ngDoCheckstill run.
Exercise
A parent component uses OnPush and passes items to a child as an input. A button handler calls this.items.push(newItem), and the view doesn't update. Why, and how would you fix it?
Show answer
push mutates the existing array, so the input's reference is unchanged and OnPush sees no reason to check the component. Assign a new array instead, for example this.items = [...this.items, newItem]. Alternatively, store the list in a signal and update it with set or update.