Start by measuring (Lighthouse, Chrome DevTools Performance panel, Angular DevTools profiler, bundle analysis), then apply targeted fixes. Load-time performance - Lazy-load feature routes and standalone components (loadChildren, loadComponent) so the initial bundle stays small. - Use @defer blocks (Angular 17+) to load heavy parts of a page — charts, editors, comments — only when visible or idle. - Analyse bundles (ng build --stats-json with esbuild-visualizer or source-map-explorer) and remove large or unused libraries; import only what you use. - Production builds with the esbuild-based builder, plus compression (Brotli or gzip) and long cache headers for hashed files on the server or CDN. - Optimise images with NgOptimizedImage (lazy loading, priority for the LCP image, proper sizes). - Consider server-side rendering or prerendering with hydration for faster first paint and better SEO. Runtime performance - Use the OnPush change detection strategy, immutable data and signals so Angular checks only components whose inputs or state changed. Newer versions also support zoneless change detection. - Use track in @for loops (or trackBy with *ngFor) so Angular reuses DOM elements instead of recreating them. - Avoid calling methods in templates that run on every change detection cycle; use pure pipes or computed signals. - Virtual scrolling (CDK ScrollingModule) for long lists; paginate large tables. - Debounce user input and cancel outdated HTTP requests with switchMap. - Run expensive non-UI work outside Angular's zone or in Web Workers. Network and data - Cache API responses where appropriate (shareReplay, HTTP caching), avoid duplicate requests, and request only needed data. Prevent memory leaks - Unsubscribe with takeUntilDestroyed, the async pipe or signals, and clean up timers and event listeners. Finally, verify each improvement with metrics (LCP, INP, CLS) and keep a performance budget in angular.json so regressions fail the build.
Ravindra Wadile · · 0 likes