← Back to Angular, page 8

angular-0147 · Hard senior

A page in your Angular app feels slow. How do you find the cause and decide what to fix first?

Measure first. Reproduce the problem, classify it as load, render or interaction, use Lighthouse and Web Vitals for load, the Chrome Performance panel and Angular DevTools profiler for runtime, and the bundle analyzer for size. Fix the biggest measured cost, then measure again.

Full answer

Guessing wastes time, so follow a loop: measure, find the largest cost, change one thing, measure again.

Step 1: classify the problem.

  • Slow load (white screen, late content): affects LCP and FCP. Tools: Lighthouse, WebPageTest, Chrome DevTools Network panel with throttling, and field data (CrUX or your own Web Vitals reporting).
  • Slow interaction (clicks lag): affects INP. Tool: the Performance panel, which shows long tasks (over 50 ms).
  • Janky scrolling or animation: layout and paint cost. Check the rendering tab and layer counts.

Step 2: find the cause by category.

Load:

  • Large bundles: run ng build --stats-json and open the result in a bundle analyzer (esbuild-visualizer, source-map-explorer). Look for a big library imported for one function, duplicated dependencies, and code that should be lazy (loadComponent, @defer).
  • Slow server or many requests: check the waterfall. Use withFetch, preconnect, HTTP caching, SSR with hydration, and image optimization (NgOptimizedImage, with priority for the LCP image).

Runtime:

  • Too much change detection: Angular DevTools Profiler records each pass and shows which components were checked and how long they took. Fixes: OnPush, signals, moving timers or scroll listeners outside the zone, or going zoneless.
  • Expensive template expressions: methods or heavy pipes called on every pass. Use computed, memoization, or pure pipes.
  • Long lists: track with a stable id in @for, virtual scrolling (cdk-virtual-scroll-viewport), pagination.
  • Heavy computation: move it to a Web Worker or chunk it.
  • Memory growth: Memory panel heap snapshots, detached DOM nodes and unsubscribed streams.

Step 3: prioritise by user impact and effort. A change that saves 800 ms on the main route beats one that saves 20 ms on a rare page.

Step 4: prevent regression: budgets in angular.json, Lighthouse CI on pull requests, and Web Vitals monitoring in production.

You can add your own marks around suspicious code with performance.mark and performance.measure. They appear in the Performance panel timeline and can be logged.

typescript
export function timed<T>(label: string, work: () => T): T {
  performance.mark(`${label}-start`);
  try {
    return work();
  } finally {
    performance.mark(`${label}-end`);
    const entry = performance.measure(label, `${label}-start`, `${label}-end`);
    console.log(label, entry.duration >= 0 ? 'measured' : 'invalid');
  }
}

const total = timed('sum-marks', () => {
  let sum = 0;
  for (let i = 0; i < 1_000_000; i++) {
    sum += i % 100;
  }
  return sum;
});
console.log('total', total);

TypeScript 5.8. The helper that wraps `performance.mark` and `performance.measure` was run in Node 24 twice. The measured times differ between runs, so the script prints only the label and whether the duration was non-negative, and both runs were identical.

Key points

  • Measure, change one thing, measure again.
  • Classify as load, interaction or jank, and choose tools for each.
  • Common fixes: lazy loading, smaller bundles, OnPush and signals, `track`, virtual scroll, workers.
  • Prevent regressions with budgets and monitoring.

Common mistakes

  • Optimising without a measurement.
  • Fixing a small cost on a rarely used page first.
  • Profiling a development build, which is slower and different from production.

Follow-ups

  • How do you reduce INP in a heavy form?
  • When would you use a Web Worker?
  • How would you catch a regression in CI?

Tips

  • Start with classification. It shows a method.
  • Use production builds for measurements.
  • Finish with prevention.

Evaluation rubric

  • Gives a measurement-led workflow.
  • Matches tools and fixes to problem types.
  • Prioritises and prevents regressions.