Sub topic 13
Rendering large lists efficiently
This chapter assumes you can record a trace in the Performance panel, read a flame chart, and know that Angular change detection runs after Zone.js reports async work. It uses Angular's @for block syntax.
Why lists get expensive
A list multiplies every per-row cost by the number of rows. The worst case is a list that gets replaced, for example by a refresh from the server. If Angular cannot tell which new item matches which old one, it destroys every row and builds it again.
In a trace, this pattern looks like one long task. Under it you see Angular's update code, then a wide Recalculate Style and Layout block. Both grow with the number of rows. The Memory panel also shows a burst of allocation and garbage collection after each refresh.
Track rows by a stable identity
The track expression tells Angular how to match items between updates. If the key is the same, Angular keeps the existing DOM nodes and component instance and only updates their inputs.
import { Component, input, signal } from '@angular/core';
interface Row {
id: number;
label: string;
}
@Component({
selector: 'app-row',
template: `<li>{{ row().label }}</li>`,
})
export class RowComponent {
row = input.required<Row>();
}
@Component({
selector: 'app-list',
imports: [RowComponent],
template: `
<button (click)="refresh()">Refresh</button>
<ul>
@for (row of rows(); track row.id) {
<app-row [row]="row" />
}
</ul>
`,
})
export class ListComponent {
rows = signal<Row[]>(this.load());
refresh() {
// New objects with the same ids, as if they came from the server.
this.rows.set(this.load());
}
private load(): Row[] {
return Array.from({ length: 5000 }, (_, i) => ({ id: i, label: `Row ${i}` }));
}
}Each refresh produces 5,000 new objects. Tracking by object identity (track row) would treat all of them as new. Tracking by row.id lets Angular reuse every node.
When tracking is not enough
Tracking reduces the cost of updating rows. It does not reduce the cost of having thousands of rows in the DOM. Options for that:
| Technique | What it reduces | Trade-off |
|---|---|---|
| `track` by id | Re-creation on update | None for most lists |
| Pagination | Rows in the DOM | Changes the interface |
| CDK virtual scroll | Rows in the DOM, to those visible | Needs fixed or estimated row height |
| `OnPush` rows | Checks per row | Inputs must be immutable |
Virtual scrolling comes from @angular/cdk/scrolling. You wrap the list in cdk-virtual-scroll-viewport and use *cdkVirtualFor. Only the visible rows exist as DOM nodes.
Check the result in a trace
Use the same data size, the same CPU throttling, and the same click each time. Compare the task duration and the Layout time, not only the total.
Exercise
A list refreshes from the server every few seconds and uses @for (item of items; track item). Each refresh returns new objects with the same ids, and the trace shows a long task with heavy Layout. What do you change, and why?
Show answer
Change it to track item.id. Object identity differs on every refresh, so Angular destroys and recreates every row. With a stable id, Angular reuses the existing nodes and only updates their inputs. Then record a new trace under the same conditions to confirm the long task is shorter.