Angular › Performance & Lazy Loading
How does using trackBy, or the track expression in the newer at for syntax, improve the performance of rendering lists?
IntermediateAnswer
Without a tracking expression, Angular assumes every item in a list might be a completely different one whenever the list reference changes, potentially destroying and recreating every single element unnecessarily. Providing a unique identifier lets Angular correctly match existing elements to the same underlying data, updating only what actually changed instead of rebuilding the entire list.
@for (product of products; track product.id) {
<app-product-card [product]="product" />
}
// Angular reuses existing elements correctly instead of recreating the entire listReal-world example A product catalog page avoids unnecessarily destroying and recreating every single product card whenever the underlying list is refreshed with mostly the same products, thanks to tracking each item by its unique product id.