Performance: ngOnInit for initialization
MEDIUM IMPACT
This affects the initial rendering speed and responsiveness by controlling when component initialization logic runs.
import { OnInit } from '@angular/core'; export class MyComponent implements OnInit { ngOnInit() { this.loadData(); } loadData() { // heavy synchronous logic } }
export class MyComponent { constructor() { this.loadData(); } loadData() { // heavy synchronous logic } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Initialization in constructor | Immediate heavy DOM changes | Multiple reflows if DOM updated synchronously | High paint cost delaying first paint | [X] Bad |
| Initialization in ngOnInit | DOM changes after inputs set | Single reflow after initialization | Lower paint cost, faster first paint | [OK] Good |