All topics
Performanceintermediate

Lazy Loading for Bundle Size

Explain lazy loading specifically as a bundle-size and initial-load-performance technique, distinct from its routing mechanics.

While lazy loading is configured through the Router (covered in depth under the Routing group), it's worth understanding specifically as a performance technique in its own right, since interviewers in performance-focused discussions often want you to reason about *why* it matters for metrics like Time to Interactive and First Contentful Paint, not just how the loadComponent/loadChildren syntax works.

It's like packing only a carry-on bag for the first leg of a trip and having the rest of your luggage shipped separately to arrive exactly when you reach your final destination, rather than hauling every suitcase you own through the airport from the very first step.

Key Concepts

1
Every byte of JavaScript in the initial bundle has to be downloaded, parsed, compiled, and executed by the browser before the application becomes interactive, and this cost scales with bundle size regardless of how fast the user's specific device or network connection is — a large bundle disproportionately hurts users on slower connections or lower-end devices, which is a real equity and reach concern for consumer-facing applications, not just an abstract performance number.
2
Lazy loading directly attacks this by ensuring the initial bundle contains only what's needed for the first meaningful paint and initial interaction, deferring everything else (admin panels, rarely-visited settings pages, entire feature areas) to be fetched only if and when the user actually navigates there — which for many real applications, where users only touch a fraction of all available routes in a typical session, can be a substantial reduction in what's downloaded for the common case.
3
A thorough interview answer connects this to measurement: bundle analyzer tools (like source-map-explorer or the Angular CLI's own --stats-json output feeding into webpack-bundle-analyzer) are how you actually verify lazy loading is working as intended — confirming a feature's code genuinely lives in its own separate chunk rather than being accidentally pulled into the main bundle through some eager import elsewhere in the codebase, which is a surprisingly common way lazy loading silently stops working without any error being thrown.
source-map-explorer--stats-json