0
0
Angularframework~8 mins

*ngFor for list rendering in Angular - Performance & Optimization

Choose your learning style9 modes available
Performance: *ngFor for list rendering
MEDIUM IMPACT
*ngFor affects how many DOM elements are created and updated, impacting rendering speed and responsiveness when displaying lists.
Rendering a dynamic list of items efficiently
Angular
<div *ngFor="let item of items; trackBy: trackById">{{item.name}}</div>

trackById(index: number, item: any): any { return item.id; }
trackBy lets Angular identify items uniquely, so only changed items update in the DOM, reducing reflows.
📈 Performance GainSingle reflow and repaint for changed items only, not the entire list
Rendering a dynamic list of items efficiently
Angular
<div *ngFor="let item of items">{{item.name}}</div>
Without trackBy, Angular recreates all DOM elements on any list change, causing many reflows and repaints.
📉 Performance CostTriggers N reflows and repaints on each update where N is the list length
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
*ngFor without trackByDestroys and recreates all nodes on list changeN reflows for N itemsHigh paint cost due to full redraw[X] Bad
*ngFor with trackByReuses existing nodes, updates only changed itemsSingle or minimal reflowsLow paint cost, partial redraw[OK] Good
Rendering Pipeline
*ngFor creates DOM nodes for each list item. Without trackBy, Angular destroys and recreates nodes on any list change, causing layout recalculations and paint. With trackBy, Angular reuses nodes, minimizing layout thrashing.
DOM Creation
Layout
Paint
Composite
⚠️ BottleneckLayout due to full DOM node recreation without trackBy
Core Web Vital Affected
INP
*ngFor affects how many DOM elements are created and updated, impacting rendering speed and responsiveness when displaying lists.
Optimization Tips
1Always use trackBy with *ngFor to uniquely identify list items.
2Avoid changing the list reference unnecessarily to prevent full re-renders.
3Keep list item templates simple to reduce paint cost.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem when using *ngFor without trackBy on a large list?
AAngular skips rendering the list entirely.
BAngular caches all DOM nodes and never updates the list.
CAngular recreates all DOM nodes on any list change causing many reflows.
DAngular only updates the first item in the list.
DevTools: Performance
How to check: Record a performance profile while updating the list. Look for long Layout and Recalculate Style tasks.
What to look for: Excessive Layout events and long scripting times indicate missing trackBy causing full DOM rebuilds.