Sub topic 12
Reducing work with OnPush and signals
This chapter assumes you can read a Performance panel trace and have used the Angular DevTools profiler. You should also know that Zone.js triggers change detection after events, timers, and HTTP responses. Here you apply the fix for a common finding: a component that is checked on every pass but rarely changes.
Why default change detection costs more
By default, Angular checks every component in the tree on each change detection pass. Any event handler or timer that Zone.js sees starts a pass. If a template calls a method such as {{ calculateTotal() }}, that method runs on every pass, even when its inputs have not changed.
In a trace, this appears as repeated tick calls with many template function calls beneath them. In the Angular DevTools profiler, the same component shows up in many bars.
OnPush limits which components are checked
ChangeDetectionStrategy.OnPush tells Angular to skip a component unless one of these happens:
- An input reference changes.
- An event handler in the component's template runs.
- A signal read in its template changes.
- You call
markForCheck()or use theasyncpipe, which calls it for you.
Skipping a component also skips its children, unless they have their own triggers.
Signals and computed values
A computed signal caches its result. It recalculates only when a signal it read has changed. This replaces the template method call with a value that is cheap to read.
import { ChangeDetectionStrategy, Component, computed, input } from '@angular/core';
@Component({
selector: 'app-order-total',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Total: {{ total() }}</p>`,
})
export class OrderTotalComponent {
items = input.required<{ price: number; qty: number }[]>();
total = computed(() =>
this.items().reduce((sum, i) => sum + i.price * i.qty, 0)
);
}The items input is a signal, so total depends on it. The reduce runs once per new items value. Other passes read the cached number.
How to apply it
- Find a component in the profiler that runs often but whose output seldom changes.
- Add
OnPushand move derived values intocomputedsignals. - Check that the UI still updates. A view that goes stale after a mutation is the usual sign of a mutated input.
- Record again and compare.
What to expect in the trace
After the change, the profiler should show fewer checks for that component, and the template function calls under tick should shorten. Do not assume a gain. The next chapters cover how to verify it with repeatable before-and-after traces.
Exercise
A parent component adds an item with this.items.push(item). The child component is OnPush and receives items as an input, but the list on screen does not update. Why, and how do you fix it?
Show answer
OnPush checks whether the input reference changed. push mutates the same array, so the reference is the same and the child is skipped. Assign a new array, for example this.items = [...this.items, item], or keep the list in a signal and call items.update(list => [...list, item]).
Exercise
A template contains {{ getTotal() }}, and the profiler shows getTotal running on every pass. Name two changes that stop it from running each time.
Show answer
Replace the method with a computed signal that reads the input signals, and set changeDetection: ChangeDetectionStrategy.OnPush on the component so Angular skips it when nothing it depends on has changed.