Tuesday, 8 April 2025

Improve Performace of Angular application

 

Great one! πŸš€ Improving the performance of an Angular application is crucial — especially as your app grows. Here’s a complete checklist to supercharge your Angular app’s speed, with practical steps πŸ‘‡


🧠 1. Use OnPush Change Detection

WhatWhy
Use ChangeDetectionStrategy.OnPush in componentsReduces unnecessary checks and DOM updates
@Component({ changeDetection: ChangeDetectionStrategy.OnPush })

πŸ“¦ 2. Lazy Load Modules

WhatWhy
Split large app into modules & lazy load routesLoads only what's needed, reduces initial bundle size

{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }

🧼 3. Use Pure Pipes

WhatWhy
Use custom pipes with pure logic (pure: true)Angular skips recalculation if inputs don’t change

✂️ 4. Minimize Bundle Size

WhatHow
Remove unused codeEnable tree shaking
Use ng build --prodMinifies, uglifies and removes dead code
Use source-map-explorerAnalyzes bundle contents
bash
ng build --configuration production npx source-map-explorer dist/**/*.js

πŸ•Ή 5. Optimize Images and Assets

StepTool
Compress imagesTinyPNG, ImageMagick
Use lazy loading for images<img loading="lazy" ...>
Serve responsive imagesUse srcset and smaller dimensions

⚙️ 6. *Use TrackBy in ngFor

WhyWithout trackBy, Angular re-renders the entire list

<tr *ngFor="let item of items; trackBy: trackByFn">

trackByFn(index: number, item: Item) { return item.id; }

πŸ—ƒ 7. HTTP Optimization

WhatHow
Avoid duplicate HTTP callsCache results in services
Use interceptorsAdd retry, auth tokens, or loading spinners
Use RxJS operators like shareReplay()For caching observable responses

🧠 8. Use Web Workers (for heavy CPU tasks)

WhatWhy
Offload CPU-heavy workPrevents UI from freezing

Angular supports Web Workers for long-running tasks like image processing, calculations, etc.


πŸ”₯ 9. Enable SSR (Server-Side Rendering)

ToolWhy
Angular UniversalImproves first load time, good for SEO
ng add @nguniversal/express-engine

🧹 10. Clean Up Subscriptions

| Why | Prevent memory leaks & slowdowns |

Use takeUntil, AsyncPipe, or unsubscribe in ngOnDestroy.


πŸš€ 11. Use Service Workers (for PWA)

  • Angular’s service worker caches static content and speeds up subsequent loads.

bash
ng add @angular/pwa

πŸ” 12. Use Performance Monitoring Tools

ToolUse
Lighthouse (Chrome DevTools)Audit performance, accessibility
Angular DevToolsProfile components, detect unnecessary renders
Web VitalsTrack CLS, FID, LCP etc.

✅ Bonus Tips

  • Avoid using any type (bad for optimization)

  • Debounce search/filter operations

  • Avoid deep nested loops and watchers

  • Break large components into smaller, focused ones

  • Prefer const & let over var

Share:

0 comments:

Post a Comment