angular
/

Angular Lazy Loading – Optimizing Performance

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

angular

Angular Lazy Loading – Optimizing Performance

What is Lazy Loading?

Lazy loading is a design pattern that delays the initialization of a resource until it is actually needed. In Angular, this means that the JavaScript for a specific feature (like an Admin Dashboard or a Code Editor) isn't downloaded until the user navigates to that feature's route. This significantly reduces the Initial Bundle Size and improves the Time to Interactive (TTI).

  1. Why Use Lazy Loading?

In large-scale applications, the 'Main' bundle can become bloated. As an Architect, you should aim for a lean initial load. Lazy loading ensures that a user only downloading the core framework and the landing page doesn't have to wait for the code of the entire 'Settings' or 'Analytics' module.

  • Faster Initial Load: Users see the first page quicker.
  • Reduced Bandwidth: Only necessary code is transferred.
  • Better Resource Management: The browser doesn't have to parse unused JavaScript.

  1. Implementing Route-Based Lazy Loading

In modern Angular (v15+), you can lazy load individual standalone components using the loadComponent property in your route configuration. For older module-based apps, you use loadChildren.

TypeScriptRead-only
1
// app.routes.ts
export const routes: Routes = [
  {
    path: 'dashboard',
    // The component is only imported when the user hits /dashboard
    loadComponent: () => import('./features/dashboard/dashboard.component')
      .then(m => m.DashboardComponent)
  },
  {
    path: 'admin',
    // Lazy loading an entire child routing configuration
    loadChildren: () => import('./features/admin/admin.routes')
      .then(m => m.ADMIN_ROUTES)
  }
];

Lazy Loading vs. Eager Loading

Eager loading is the default behavior where all components are imported at the top of the file and bundled into the main package.

FeatureEager LoadingLazy Loading
Initial LoadSlow (Large bundle)Fast (Small main bundle)
User ExperienceWait at startSmall wait during navigation
ImplementationStandard 'import' at topDynamic 'import()' in routes
Best ForSmall apps / Critical pathsLarge Feature modules / Admin panels

  1. Preloading Strategies

To avoid a 'loading' delay when a user clicks a link, you can use a Preloading Strategy. This tells Angular to download the lazy-loaded modules in the background after the initial app has loaded and the user is idle.

TypeScriptRead-only
1
bootstrapApplication(AppComponent, {
  providers: [
    provideRouter(routes, withPreloading(PreloadAllModules))
  ]
});

Test Your Knowledge

Q1
of 3

Which property in the Route object is used to lazy load a single standalone component?

A
component
B
loadChildren
C
loadComponent
D
importComponent
Q2
of 3

What is the primary benefit of Lazy Loading for the user?

A
It makes the application logic more secure
B
It reduces the initial download size of the application
C
It prevents the need for a database
D
It automatically optimizes images
Q3
of 3

Which Preloading Strategy downloads all lazy modules as soon as the app starts in the background?

A
NoPreloading
B
PreloadAllModules
C
LazyPreload
D
IdlePreload

Frequently Asked Questions

Does lazy loading affect SEO?

If you are using Server-Side Rendering (SSR) with Angular Universal, SEO is not negatively affected. For standard client-side apps, modern search engines can still crawl lazy-loaded routes, but SSR is preferred for critical content.

What is 'loadChildren' used for now?

While 'loadComponent' is for single components, 'loadChildren' is still used when you want to load a group of routes (a sub-routing config) or a full NgModule for a complex feature.

How do I see if lazy loading is working?

Open the browser's Network tab and filter by 'JS'. When you navigate to a lazy-loaded route, you should see a new '.js' chunk file being downloaded.

Previous

angular route guards

Next

angular forms

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.