Performance: ngAfterViewInit for view ready
MEDIUM IMPACT
This lifecycle hook affects when DOM-dependent code runs, impacting interaction readiness and rendering stability.
ngAfterViewInit() {
const element = document.querySelector('.my-element');
element.style.width = '100px';
}ngOnInit() {
const element = document.querySelector('.my-element');
element.style.width = '100px';
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| DOM manipulation in ngOnInit | Early DOM access, may fail or cause forced reflows | Multiple reflows if elements not ready | Higher paint cost due to layout thrashing | [X] Bad |
| DOM manipulation in ngAfterViewInit | DOM fully ready, safe to access | Single reflow triggered | Lower paint cost, smoother rendering | [OK] Good |