Performance: Dynamic component loading
MEDIUM IMPACT
This affects the initial page load speed and interaction responsiveness by controlling when and how components are loaded and rendered.
import { ViewContainerRef, Component } from '@angular/core'; @Component({ selector: 'app-root', template: `<ng-template #container></ng-template>` }) export class AppComponent { constructor(private vcr: ViewContainerRef) {} async loadComponent() { const { ComponentA } = await import('./component-a'); this.vcr.clear(); this.vcr.createComponent(ComponentA); } }
import { ComponentA } from './component-a'; @Component({ selector: 'app-root', template: `<component-a></component-a>` }) export class AppComponent {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Static import and render | High (all components created at start) | Multiple (due to many components) | High (all painted immediately) | [X] Bad |
| Dynamic import with lazy loading | Low (only loaded components created) | Single or few (on-demand) | Low (only painted when needed) | [OK] Good |