Performance: Lifecycle execution order mental model
MEDIUM IMPACT
This concept affects how quickly Angular components initialize and update, impacting the time to interactive and smooth user experience.
ngOnInit() {
this.scheduleHeavyTask(); // defer heavy work
}
scheduleHeavyTask() {
if ('requestIdleCallback' in window) {
requestIdleCallback(() => {
// heavy computation deferred
});
} else {
setTimeout(() => {
// heavy computation deferred
}, 0);
}
}ngOnInit() {
for(let i = 0; i < 1000000; i++) {
// heavy computation
}
}
ngAfterViewInit() {
// more heavy DOM manipulation
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy logic in ngOnInit | Minimal DOM | 0 | Low | [X] Bad |
| Heavy logic deferred outside lifecycle hooks | Minimal DOM | 0 | Low | [OK] Good |
| Multiple DOM updates in ngOnChanges and ngAfterViewChecked | Multiple DOM updates | 2+ | High | [X] Bad |
| Batched DOM updates with requestAnimationFrame | Single DOM update | 1 | Medium | [OK] Good |