Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 12 / Topic 1 of 6

Measure first: a genuinely slow list, proven before any fix

Real profiling (the React DevTools Profiler tab, in a real browser) reports render durations in milliseconds - numbers that genuinely vary by machine, load, and browser build, which is exactly why this course never asserts on one directly. What is deterministic, and what every test in this part asserts on instead, is a count: how many times a render happened, how many times an expensive calculation ran. examples/01_slow_list_before_memoization.jsx's computeSurgeMultiplier does real, measurable work (a loop, not a placeholder) and increments a shared counter on every call:

// examples/01_slow_list_before_memoization.jsx
export const computeCount = { value: 0 };

export function computeSurgeMultiplier(basePrice) {
  computeCount.value++;
  let total = 0;
  for (let i = 0; i < 20000; i++) {
    total += Math.sqrt(basePrice + i) % 7;
  }
  return 1 + (total % 30) / 100;
}

export function RestaurantListSlow() {
  const [note, setNote] = useState("");
  return (
    <div>
      <input aria-label="delivery note" value={note} onChange={(e) => setNote(e.target.value)} />
      <ul>
        {RESTAURANTS.map((r) => {
          const surge = computeSurgeMultiplier(r.basePrice);
          return <li key={r.id}>{r.name}: ×{surge.toFixed(2)}</li>;
        })}
      </ul>
    </div>
  );
}

With 200 restaurants, mounting this really runs the calculation exactly 200 times (examples/01_slow_list_before_memoization.test.jsx) - unsurprising. The genuinely slow part: typing a single character into the delivery note input, which has nothing whatsoever to do with any restaurant's price, really reruns the calculation another 200 times. Two more keystrokes really cost 200 more each. On real hardware this is a felt, visible slowdown while typing - (varies) by machine, which is exactly why the test asserts computeCount.value instead of a stopwatch. Not one of those 200 recalculations needed to happen - this is the wasted work the rest of this part fixes, proven with numbers before a single memoization API has been written.

Done here? The practice set for Part 12 — including the solutions — is on the part page.

Search the guide