Sub topic 2
Default change detection and its cost
This chapter assumes you can build components with templates and bindings, and that you know how inputs work. It also assumes you know that Zone.js tells Angular when asynchronous work finishes. Here you will see what Angular does on each of those notifications, and why that work becomes expensive in large applications.
What the default strategy does
Change detection is the process that compares the current values of template bindings with the values rendered last time, then updates the DOM where they differ. Every component starts with ChangeDetectionStrategy.Default. You don't have to write it.
With the default strategy, a notification from Zone.js starts a pass from the root of the application. The pass visits every component in the tree, top to bottom, whether or not anything inside it changed. For each component, Angular re-evaluates every expression in its template and compares the result with the previous value. If a value differs, Angular updates that part of the DOM.
The strategy is safe because it assumes nothing. A component can mutate objects, change plain fields, or read a service's state, and the view still catches up on the next pass. The cost is that Angular cannot skip any work.
What triggers a pass
Zone.js patches browser APIs, so Angular is told after any of these:
- DOM events such as
clickandinput setTimeoutandsetIntervalcallbacks- Promise resolutions
- HTTP responses and other XHR or
fetchcompletions
The trigger says only that something might have changed. It doesn't say what. Angular therefore checks everything.
Seeing the cost
This example logs each time a row's binding is evaluated. The row uses a getter, which runs on every check.
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-row',
template: `<li>{{ label }}</li>`,
})
export class RowComponent {
@Input({ required: true }) name!: string;
get label(): string {
console.log('checked', this.name);
return this.name.toUpperCase();
}
}
@Component({
selector: 'app-list',
imports: [RowComponent],
template: `
<button (click)="clicks = clicks + 1">Clicked {{ clicks }}</button>
<ul>
@for (n of names; track n) {
<app-row [name]="n" />
}
</ul>
`,
})
export class ListComponent {
clicks = 0;
names = ['Ada', 'Grace', 'Linus'];
}Click the button and open the browser console. Each click logs a checked line for all three names, even though the click changed only clicks and the rows depend on neither it nor anything else that changed. In development mode you will see each name twice. Angular runs a second verification pass that confirms no binding changed during the first one.
Three rows is trivial. Now picture a table with 500 rows and 10 bindings per row. A mouse move handler, a polling timer, or a keystroke in a search box then forces about 5,000 expression evaluations. The work happens on the main thread, between the event and the next paint.
Where the cost grows
The cost of a pass is roughly the number of bindings in the whole tree. These factors make it worse:
- Large trees. Dashboards, data grids, and long lists multiply the bindings per pass.
- Frequent triggers.
mousemove,scroll,setInterval, and chatty WebSocket handlers each start a full pass. - Expensive expressions. Template method calls and getters run on every pass. A method that filters or sorts an array runs again each time, whatever caused the pass.
- Unrelated events. A click in one corner of the page re-checks components in every other corner.
Sluggish typing, dropped frames while scrolling, and long tasks in the browser profiler are the usual symptoms.
Why this motivates OnPush
Most components in a real application depend only on their inputs. When none of those inputs changed, re-evaluating their templates produces the same result. The default strategy has no way to know that, so it checks them anyway.
OnPush lets a component declare that it depends only on certain things: its inputs, events from its own template, and explicit notifications. Angular can then skip the component, and its whole subtree, during passes where none of those happened. The next chapter shows how to opt in.
Exercise
A page has a root component with 40 child components, and each child has 25 bindings. A timer fires setInterval every 100 ms and updates one field in a single child. With default change detection, roughly how many binding evaluations occur per second, and why?
Show answer
Each timer tick starts a full pass, and the pass checks every component. That is 40 × 25 = 1,000 bindings per pass (plus the root's own), and 10 passes per second, so about 10,000 evaluations per second. Development mode adds a second verification pass. The default strategy cannot tell that only one child changed, so it re-checks all of them.