Performance: Lazy loading modules with routes
HIGH IMPACT
This affects the initial page load speed by deferring loading of feature modules until their routes are accessed.
const routes: Routes = [ { path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) }, { path: 'dashboard', component: DashboardComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
import { FeatureModule } from './feature/feature.module'; const routes: Routes = [ { path: 'feature', loadChildren: () => FeatureModule }, { path: 'dashboard', component: DashboardComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager loading all modules | High (all components loaded) | Multiple (due to large DOM) | High (large initial paint) | [X] Bad |
| Lazy loading modules with routes | Low initially (only active route DOM) | Single or few (on route change) | Low initial paint cost | [OK] Good |