Sub topic 3
Finding Expensive Template Expressions
Assume you know Angular components, templates, TypeScript, and basic Chrome DevTools. This chapter focuses on finding repeated work and checking whether an optimization actually helps.
Find work in template expressions
Angular evaluates template expressions during change detection. A method call such as {{ total() }} can run many times, even when the result has not changed. If the method filters a large array, the repeated work can appear as a wide block in the Performance panel flame chart.
Prefer a computed signal when the result depends on reactive input. Angular recalculates it when its input changes.
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, item) => sum + item.price * item.qty, 0)
);
}A computed signal caches its result until a dependency changes. OnPush also limits checks to relevant input changes, events, or signal updates.
Render lists efficiently
Give Angular a stable identity for each row. Without tracking, replacing an array can cause unnecessary DOM work.
@for (row of rows; track row.id) {
<app-row [row]="row" />
}Tracking does not reduce the cost of calculating the array. It reduces the cost of updating the DOM.
Keep frequent events outside Angular
Scroll, resize, and pointer events can fire many times per second. Register listeners with runOutsideAngular when their work does not update the view. Re-enter Angular only when a visible state change is needed.
this.zone.runOutsideAngular(() => {
window.addEventListener('scroll', this.onScroll, { passive: true });
});Remove the listener in ngOnDestroy, or it can retain the component and create a memory leak.
Check memory and load cost
Use the Memory panel to take heap snapshots before and after repeatedly creating and destroying a view. Growing retained component instances, listeners, or subscriptions suggest a leak.
Use the Network panel with cache disabled to inspect JavaScript transfer size and loading time. Large lazy chunks, source maps, or duplicate dependencies can delay startup even when runtime traces look fast.
Verify the change
Record the same user action before and after the change. Use the same browser conditions and throttling. Compare the interaction duration, long tasks, scripting time, and memory trend. Add a User Timing label when the trace needs an application-specific boundary.
performance.mark('filter-start');
const result = this.items.filter(item => item.name.includes(term));
performance.measure('filter', 'filter-start');Exercise
Why should a list use track row.id instead of tracking by object identity?
Show answer
A stable ID lets Angular reuse the existing DOM node when the row object or array changes.