Sub topic 5
The Default strategy and its cost
This chapter assumes you know the component tree, how template bindings are compared, and how Zone.js starts a change detection cycle. If those are unfamiliar, read the earlier chapters first.
What Default means
Every component uses ChangeDetectionStrategy.Default unless it says otherwise. Under this strategy, Angular treats a component as possibly changed on every cycle. It checks that component's template bindings each time, no matter what triggered the cycle.
A cycle starts at the root component and visits every component below it. For each one, Angular evaluates the template expressions and compares each result with the value from the previous check. If a value differs, Angular updates that part of the DOM.
Why it is the default
Default is safe. You can mutate objects, change fields from a timer, or update state in a promise callback, and the view still catches up. You do not have to tell Angular what changed.
Where the cost comes from
The cost of a cycle is the number of bindings in the tree multiplied by the cost of evaluating each one. Two things make it grow:
- Tree size. Every extra component adds bindings to every cycle, even if its data never changes.
- Frequency. Zone.js starts a cycle after every event, timer and resolved promise, including ones unrelated to your view.
Expensive expressions make this worse, because Angular runs them on every cycle. The component below shows the problem.
import { Component } from '@angular/core';
@Component({
selector: 'app-report',
template: `
<button (click)="noop()">Unrelated click</button>
<p>Total: {{ total() }}</p>
`,
})
export class Report {
private items = [3, 5, 8];
total(): number {
console.log('total() evaluated');
return this.items.reduce((sum, n) => sum + n, 0);
}
noop() {}
}Click the button and total() evaluated is logged, even though items did not change. In development mode Angular also runs a second verification pass, so you may see the message twice per cycle.
The method here is cheap. If it filtered a list of 10,000 items, it would run on every event in the application.
What Default does well
The cost is easy to overstate. Comparing a binding is a cheap operation, and most applications with a few hundred bindings run Default without any visible problem. Reaching for OnPush on every component adds rules you must follow, such as immutable updates, for little gain on a small tree.
The following table summarises when the cost matters.
| Situation | Effect under Default |
|---|---|
| Small tree, simple bindings | Cost is negligible |
| Large tree, such as a data grid with many rows | Each event checks every row |
| High-frequency events, such as `mousemove` or scroll | A full cycle per event |
| Expensive template expressions | Re-evaluated every cycle |
Deciding when to move on
Do not optimise until you have evidence. Profile first, using the tools covered later in this tutorial. If a cycle takes long enough to cause dropped frames, you have two options: reduce how often cycles run, or reduce how much of the tree each cycle visits. The next chapter covers OnPush, which does the second.
Exercise
A Default component's template calls {{ expensiveFilter() }}. The user types in an unrelated text field elsewhere in the application. Does expensiveFilter() run, and why?
Show answer
Yes. The keystroke is an event that Zone.js reports to Angular, which starts a cycle from the root. Under Default, every component is checked on each cycle, so the template expression is re-evaluated even though its data did not change. Moving the result into a field or a computed signal avoids the repeated work.