track and list rendering performance

Intermediate · 9 min

Why @for requires a track expression, and *ngFor's trackBy before it — both answer the same identity question.

Before thisBuilt-in control flow: @if, @for, and @switchOnPush change detection

Angular University · 9 min

track and list rendering performance

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Angular UniversityReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Without a stable identity to track by, Angular can only match old and new list items by position — which produces wrong results the instant a list reorders, inserts, or removes from the middle.
  • `track item.id` (or a trackBy function returning the same) lets Angular recognize "this is the same item, just moved or updated" instead of treating every change as a full remove-and-recreate.
  • The cost of getting this wrong isn't a visible bug most of the time — it's wasted DOM churn (destroying and recreating elements that didn't need to change), which only shows up as a performance problem under real load.

Matching old items to new ones needs an identity, not a position

Every time a list-backed @for (or the legacy *ngFor) re-renders, Angular has to answer a specific question: of the DOM elements already on the page, which ones correspond to which items in the new version of the list? Get that answer right, and Angular can update or move existing elements in place. Get it wrong, and it falls back to the safest possible assumption — treat every item as new, destroy the old elements, build fresh ones from scratch.

Without being told otherwise, the only thing Angular has to go on is position: item at index 3 in the old list corresponds to item at index 3 in the new one. That assumption holds exactly as long as nothing reorders, and breaks the moment anything does.

track: giving Angular a real identity to match on

@for (item of items; track item.id) {
  <li>{{ item.name }}</li>
}

track item.id tells Angular: match old and new list entries by this value, not by position. Remove an item from the middle of the list, and Angular can now see exactly which specific item is gone — every other item’s id is unchanged, so their existing DOM elements are reused and simply repositioned, rather than every item after the removed one being treated as a wholesale change just because its index shifted.

*ngFor’s equivalent, from the legacy-directives lesson, is a trackBy function rather than an inline expression:

<li *ngFor="let item of items; trackBy: trackById">{{ item.name }}</li>
trackById(index: number, item: Item) {
  return item.id;
}

Functionally identical job, different syntax — both exist to answer the same “what actually corresponds to what” question.

Why track $index (or no trackBy at all) quietly costs you

It’s tempting to reach for track $index when nothing else about an item is obviously unique, but this re-introduces the exact position-based matching problem track exists to solve. Insert or remove anything from the middle of the list, and every subsequent item’s index shifts — which track $index reports to Angular as every one of those items having a “new” identity, even though the actual item objects are unchanged. The visible result can look correct (the right content ends up on screen either way), which is exactly why this is easy to miss: the cost is in how much DOM gets needlessly destroyed and recreated to get there, not in anything a quick glance at the rendered page would reveal.

Seeing the DOM churn for yourself

This is worth verifying rather than taking on faith, since the cost is invisible from the rendered output alone. Open a browser’s DevTools, enable the option to highlight repainted regions (or inspect the Elements panel for which nodes get recreated), and compare track $index against track item.id on the same list while inserting an item at the front. With track $index, every existing list item’s DOM node gets torn down and rebuilt, visible as a flash across the whole list; with track item.id, only the genuinely new node is created, and the rest simply shift position with no rebuild at all. That visible difference is the entire argument for taking track’s identity requirement seriously rather than treating it as boilerplate the compiler happens to insist on.

This lesson’s video demonstrates the new @for block’s track requirement directly, from a channel whose whole catalog focuses on Angular specifically; the linked “deeper” video covers the equivalent trackBy performance case for *ngFor, worth watching if you’re working in a codebase still on the legacy directive syntax.

Check yourself

  1. 01A `@for` loop renders a list of 1000 items with `track $index` instead of `track item.id`. If the first item is removed from the underlying array, what does Angular do to the remaining 999 DOM elements?

    With `track $index`, every item's identity is tied to its position, not itself — so removing the first item shifts every other item's index down by one, and Angular sees that as every single position from 1 through 999 now holding a "different" tracked value. It ends up updating or recreating far more DOM than actually changed, when in reality only one item needed to be removed and the rest just needed to shift visually.

  2. 02Why is this considered a performance problem rather than a correctness problem in most cases?

    Because `@for` still renders the right content — the list still shows the right items in the right order, from the user's perspective, position-based tracking just gets there by doing much more DOM destruction and recreation than necessary. It's not silently wrong the way a stale closure or a missed change-detection cycle would be; it's wasteful, which only becomes visible under real load (a long list, frequent updates) rather than in a quick manual test.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark