09

Performance Engineering

Profiling, virtualization, memory leaks

GoalMeasure before optimizing. Use the right tool (Profiler, Lighthouse, DevTools Memory), fix real bottlenecks, and hunt the memory leaks we've described since Level 2.

Lessons

Measure first — always

The #1 rule: never optimize by guessing. Intuition is wrong more often than right.

  • Chrome DevTools → Performance — flame chart of main thread, finds long tasks
  • React DevTools → Profiler — which components re-rendered, how often, why
  • Lighthouse / Network tab — loading, bundle size, Core Web Vitals

Workflow: reproduce → record → find biggest cost → fix it → measure again.

Core Web Vitals

  • LCP (Largest Contentful Paint) — loading speed. Good: < 2.5s. Fix: images, bundle size, server.
  • INP (Interaction to Next Paint) — responsiveness. Good: < 200ms. Fix: don't block the main thread.
  • CLS (Cumulative Layout Shift) — visual stability. Fix: image dimensions, font fallbacks.

INP directly maps to Level 3's "blocking the main thread". Heavy synchronous work in a click handler → high INP.

Virtualization

For long lists, the fix is NOT memoizing each row. The fix is only rendering visible rows.

Full list (10,000 rows) → 10,000 DOM nodes → freezes
Virtualized → ~25 DOM nodes visible → constant, smooth

Libraries: react-window, @tanstack/react-virtual.

memo on list rows treats the symptom (re-renders). Virtualization treats the cause (too many DOM nodes).

Memory Leak Hunting

Symptom: app slows down over time, memory climbs, never drops.

Diagnosis: Chrome DevTools → Memory → Heap Snapshots. Take one, do the suspect action several times, take another. Compare — objects that should be gone but aren't = leak.

  • setInterval/setTimeout never cleared
  • addEventListener never removed
  • WebSocket/observable subscription never unsubscribed

Fix: useEffect cleanup:

js
useEffect(() => {
  const id = setInterval(tick, 1000);
  window.addEventListener("resize", onResize);
  return () => {              // cleanup severs the references
    clearInterval(id);
    window.removeEventListener("resize", onResize);
  };
}, []);

Challenges1

Write working code. Run to see output. Check to grade.

CH1
Diagnose the problem
Medium

For each bug report, log the correct diagnosis: "loading", "blocking", or "leak".

(a) "Page takes 8 seconds to show anything" (b) "Clicking Sort freezes the UI for 1 second" (c) "App is fast at first but slows down after 30 minutes"

JavaScript

References