What is lazy loading in Angular and why is it useful for performance?
Short answer
Lazy loading means only downloading the code for a specific feature or route when the user actually navigates to it, instead of including everything in the app's initial bundle.
Advertisement
AModel answer
Lazy loading means only downloading the code for a specific feature or route when the user actually navigates to it, instead of including everything in the app's initial bundle. This makes the very first page load faster, since the browser only needs to download the code it actually needs right away.
const routes: Routes = [
{ path: 'orders', loadChildren: () => import('./orders/orders.routes') }
];
// The orders feature's code is only downloaded when a user visits the orders routeReal-world example
A large admin panel only downloads the code for its rarely visited reports section the first time a user actually navigates there, thanks to lazy loading that specific feature, keeping the initial page load fast for everyone.
Likely follow-ups
- How much smaller does an app's initial load usually get after adding lazy loading?
- What tool shows you how big each lazy loaded chunk actually is?
Related topics