Performance: Lazy Loading, @defer and Bundle Size

Angular 16 min min read Updated: Nov 16, 2026 Advanced
Performance: Lazy Loading, @defer and Bundle Size
Advanced Topic 2 of 5

Measure First

  • Run Lighthouse in Chrome DevTools for load speed and Core Web Vitals
  • Open the Network tab to see download sizes
  • Use the Angular DevTools profiler to see slow components
bash g build # look at the table of chunk sizes npm install -D source-map-explorer ng build --source-map npx source-map-explorer dist/my-app/browser/*.js

1. Lazy Load Routes

typescript { path: 'admin', loadChildren: () => import('./admin/admin.routes').then(m => m.ADMIN_ROUTES) }, { path: 'reports', loadComponent: () => import('./reports/reports').then(m => m.Reports) },

Only the first page's code is downloaded at start. Other pages load when visited. Add preloading for a smoother feel:

typescript provideRouter(routes, withPreloading(PreloadAllModules))

2. @defer: Load Parts of a Page Later

html <h1>Product</h1> @defer (on viewport) { <app-reviews [productId]="id()" /> } @placeholder { <div class="skeleton" style="height: 300px"></div> } @loading (minimum 300ms) { <app-spinner /> } @error { <p>Could not load reviews.</p> }
TriggerLoads when
on idleBrowser is idle (default)
on viewportPlaceholder enters the screen
on interactionUser clicks or presses key
on hoverMouse hovers
on timer(3s)After 3 seconds
when conditionExpression becomes true
prefetch on idleDownload early, show later
Rules: The deferred component must be standalone and not referenced elsewhere in the same file's imports for eager use, or Angular cannot split it out.

3. Use track in @for

html @for (item of items(); track item.id) { <app-row [item]="item" /> }

With a good track key, Angular reuses existing DOM rows when a list changes instead of rebuilding all.

4. OnPush, Signals and computed

typescript // computed caches. It recalculates only if the inputs change total = computed(() => this.items().reduce((s, i) => s + i.price * i.qty, 0));
Avoid heavy template calls: Do not write {{ calculate() }} with slow work in a method; it can run often. Use computed or a pure pipe.

5. Optimize Images

html <img ngSrc="hero.jpg" width="1200" height="600" priority alt="Hero"> <img ngSrc="card.jpg" width="400" height="300" alt="Card"> <!-- lazy by default -->
  • Use modern formats (WebP or AVIF)
  • Set width and height to avoid layout shift
  • Mark only the main (LCP) image as priority

6. Control Bundle Size with Budgets

json "budgets": [ { "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" }, { "type": "anyComponentStyle", "maximumWarning": "4kB", "maximumError": "8kB" } ]

The build warns or fails when you exceed these sizes, which stops accidental growth.

7. Smaller Imports

  • Import only what you need (RxJS operators from rxjs, not whole libraries)
  • Avoid huge date or utility libraries when Intl and native methods work
  • Remove unused dependencies and polyfills
  • Prefer @defer for charts, editors and maps

8. Other Quick Wins

IdeaBenefit
Debounce search and scroll handlersFewer calls
Virtual scrolling (cdk-virtual-scroll-viewport)Render only visible rows of huge lists
HTTP caching and shareReplayFewer requests
Server-side rendering (next lesson)Faster first paint and better SEO
Gzip or Brotli on the serverSmaller downloads
Make it a habit: Add a Lighthouse check to your CI so a slow change is caught before release.

Conclusion

Measure, then lazy load routes, defer heavy blocks, track lists, use OnPush and signals, optimize images and set budgets. Small steps add up to a much faster app.

Quick Summary: Lazy routes + @defer + track + OnPush + NgOptimizedImage + budgets.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators