Angular course · Module 12: Security and Performance
Performance Optimization
In this lesson4
Change Detection OnPush
OnPush is like guards who only check when someone knocks at the gate:
@Component({
selector: 'app-samurai-card',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="card">
<h3>{{ samurai().name }}</h3>
<p>Clan: {{ samurai().clan }}</p>
</div>
`
})
export class SamuraiCardComponent {
// Component updates ONLY when:
// 1. samurai() signal changes reference
// 2. An event (click, input) is triggered
// 3. Async pipe receives a new value
// 4. We manually call cdr.markForCheck()
samurai = input.required<Samurai>();
}Lazy Loading Components with @defer
<!-- Load when element enters viewport -->
@defer (on viewport) {
<app-heavy-chart [data]="chartData()" />
} @placeholder {
<div class="chart-placeholder">Chart will load when you scroll down...</div>
} @loading (minimum 500ms) {
<app-spinner />
} @error {
<p>Error loading chart</p>
}
<!-- Load after interaction -->
@defer (on interaction) {
<app-advanced-editor />
} @placeholder {
<button>Click to load editor</button>
}
<!-- Load after a delay -->
@defer (on timer(2000)) {
<app-recommendations />
} @placeholder {
<p>Loading recommendations...</p>
}
<!-- Conditional loading -->
@defer (when isAdmin()) {
<app-admin-panel />
}Virtual Scrolling
For long lists:
import { ScrollingModule, CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
@Component({
imports: [ScrollingModule],
template: `
<cdk-virtual-scroll-viewport itemSize="50" class="viewport">
<div *cdkVirtualFor="let samurai of samuraiList()" class="item">
{{ samurai.name }}
</div>
</cdk-virtual-scroll-viewport>
`,
styles: [`
.viewport {
height: 400px;
}
.item {
height: 50px;
}
`]
})
export class VirtualListComponent {
samuraiList = signal<Samurai[]>([]); // Even 10,000 elements!
}NgOptimizedImage
import { NgOptimizedImage } from '@angular/common';
@Component({
imports: [NgOptimizedImage],
template: `
<!-- Automatic lazy loading, srcset, preconnect -->
<img
ngSrc="/images/samurai.jpg"
width="800"
height="600"
priority
placeholder="/images/samurai-placeholder.jpg"
>
<!-- Fill mode for responsive images -->
<div class="image-container">
<img ngSrc="/images/background.jpg" fill>
</div>
`,
styles: [`
.image-container {
position: relative;
width: 100%;
height: 300px;
}
`]
})
export class OptimizedImageComponent {}Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. When does a component with ChangeDetectionStrategy.OnPush update?
2. What does @defer (on viewport) do in Angular 17+?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Complete the component with the OnPush optimization.
- Vertical ordering
Order the lines of an @defer block that lazily loads a chart (top to bottom):
- Horizontal ordering
Arrange the image tag with NgOptimizedImage in the correct order (from the left):
- Click in order
Build the opening tag of a virtual scroll viewport (click in order):