Sub topic 11
Finding expensive template expressions
This chapter assumes you can record a trace in the Performance panel, read a flame chart, and recognize change detection in a trace. It also assumes you know basic Angular templates and the Angular DevTools profiler.
Why template expressions cost so much
Angular evaluates every binding in a template on each change detection pass. A binding such as {{ total() }} or [class.active]="isActive(item)" runs again each time. If the expression calls a method that loops over data, sorts an array, or builds a new object, that work repeats on every pass, not only when the data changes.
A default-strategy component is checked after every event Zone.js reports: a click, a timer, an HTTP response. A slow expression in a busy component then shows up as a long task on many unrelated interactions.
Locating the expression in a trace
- Record a trace while you do the slow interaction, such as typing in a search box.
- Find the long task and expand it in the flame chart.
- Follow the call stack down through Angular's change detection frames until you reach a function from your own code.
- If you built with source maps, the frame shows your component and method name. Select it to see the file and line.
Look for the same function name repeating many times inside one task. A method called once per row, or once per binding, multiplies its cost.
Confirming with Angular DevTools
Open the Angular DevTools profiler, record the same interaction, and look at the bar chart of change detection cycles. A component with a tall bar is slow to check. Select it to see which component it is and how long it took. This tells you which component to open. The Performance panel tells you which function inside it is responsible.
Labeling suspect code
If the flame chart does not make the function obvious, wrap it with User Timing marks. They appear in the Timings track.
filterItems(term: string) {
performance.mark('filter-start');
const result = this.items.filter(i => i.name.includes(term));
performance.measure('filter', 'filter-start');
return result;
}Count the filter entries in one task. If the entry appears dozens of times for one keystroke, the template is calling the method on each pass.
The usual fix
Move the calculation out of the template. A computed signal recalculates only when its inputs change, and an OnPush component skips checks when nothing it depends on has changed.
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)
);
}Calling total() in the template is cheap here. The signal returns its cached value until items changes.
Exercise
A trace shows one filter User Timing entry repeated 40 times inside a single long task triggered by one click. What does this suggest, and what is a likely fix?
Show answer
The template calls the method on every binding evaluation, and change detection evaluates it repeatedly in one pass or across rows. Move the calculation into a computed signal, or precompute it when the data changes, and use OnPush so the component is not rechecked needlessly. Then record again and compare.