← Back to Angular, page 2

angular-0038 · Hard senior

How do you find and fix performance problems in a large Angular application?

Measure first with Lighthouse, the browser Performance panel, Angular DevTools, and a bundle analysis, then fix the biggest cost. Typical fixes are lazy loading and @defer for bundle size, OnPush with signals and stable track keys for runtime, NgOptimizedImage and SSR for loading, and virtual scrolling for very long lists.

Full answer

Start with a metric and a measurement, not with a favourite trick. Which problem is it: slow first load, slow interaction, or slow scrolling and updates?

Loading problems (Largest Contentful Paint, bundle size):

  • Analyse the bundle. ng build --stats-json and an esbuild bundle analyser show what is large. Set budgets in angular.json so a build fails when the initial bundle grows.
  • Lazy load routes (loadComponent, loadChildren) and defer heavy parts of a page with @defer.
  • Replace large libraries (for example a full date library) with smaller ones, or import only what is needed.
  • Use NgOptimizedImage: it enforces width and height, lazy loads images, and the priority attribute marks the LCP image with a preload hint.
  • Consider SSR or prerendering for first paint and SEO.

Runtime problems (slow clicks, typing, scrolling):

  • Record in the browser Performance panel and Angular DevTools profiler to see which components are checked and for how long.
  • Use OnPush and signals so fewer components are checked, or go zoneless.
  • Avoid calling functions in templates that do heavy work. Use computed or a pure pipe.
  • Give every @for a stable track key.
  • For very long lists, use the CDK virtual scroll so only visible rows exist in the DOM.
  • Coalesce events (provideZoneChangeDetection({ eventCoalescing: true })) and debounce noisy inputs.
  • Move heavy calculation to a Web Worker.

Prove the result: run the same measurement after the change, ideally in CI with a budget or a Lighthouse check.

Trade-off notes: every optimisation adds complexity. A 5 KB saving may not be worth a new loading state.

typescript
import { NgOptimizedImage } from '@angular/common';
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';

interface Photo {
  id: number;
  title: string;
  src: string;
}

@Component({
  selector: 'app-gallery',
  imports: [NgOptimizedImage],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <img ngSrc="/images/hero.jpg" width="1200" height="400" priority alt="Campus" />

    <input #box placeholder="Filter" (input)="filter.set(box.value)" />

    @for (p of visible(); track p.id) {
      <figure>
        <img [ngSrc]="p.src" width="300" height="200" [alt]="p.title" />
        <figcaption>{{ p.title }}</figcaption>
      </figure>
    } @empty {
      <p>No photos match.</p>
    }
  `,
})
export class GalleryComponent {
  readonly photos = signal<Photo[]>([
    { id: 1, title: 'Library', src: '/images/library.jpg' },
    { id: 2, title: 'Canteen', src: '/images/canteen.jpg' },
  ]);
  readonly filter = signal('');

  // Computed once per change, not on every change detection pass.
  readonly visible = computed(() =>
    this.photos().filter((p) => p.title.toLowerCase().includes(this.filter().toLowerCase())),
  );
}

Angular 20.3. The image paths are examples. `NgOptimizedImage` warns in development if width and height are missing or the LCP image lacks `priority`.

Key points

  • Measure first. Use Lighthouse, Performance panel, Angular DevTools, and bundle analysis.
  • Bundle: lazy routes, `@defer`, budgets, smaller libraries.
  • Runtime: OnPush, signals, `track`, `computed` instead of template functions, virtual scroll, zoneless.
  • Loading: `NgOptimizedImage`, SSR, prerendering.

Common mistakes

  • Adding OnPush everywhere before measuring, and then fixing stale-view bugs for no gain.
  • Calling a sorting or filtering function in the template, which runs on every check.
  • Lazy loading everything, including tiny components, which adds requests and delays.

Follow-ups

  • How would you set a performance budget and enforce it in CI?
  • How do you find which component is slow in Angular DevTools?
  • What is the effect of `priority` on `NgOptimizedImage`?

Tips

  • Begin with the measurement method. This is what separates senior answers from lists of tricks.
  • Tie every fix to a metric (LCP, INP, bundle size).
  • Say what you would not optimise.

Evaluation rubric

  • Starts with measurement and a target.
  • Separates loading and runtime problems.
  • Gives specific Angular tools and trade-offs.