awesome-everything RU
↑ Back to the climb

Browser & Frontend Runtime

Putting it together: code and trace reading

Crux Read four browser snippets — microtask ordering, layout thrash, a hydration mismatch, and a blocking handler — and pick the behaviour or the highest-leverage fix.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

The trace tells you where the time went; the code tells you why. Read each snippet the way you would in a review for a slow page, then pick the behaviour or the fix a senior engineer reaches for first.

Goal

Practise mapping code back to the layer it stresses: the event loop’s queue ordering, the render pipeline’s layout/paint coupling, hydration determinism, and the INP budget on the main thread.

Snippet 1 — queue ordering

console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
queueMicrotask(() => console.log("D"));
console.log("E");
Quiz

What order does this log, and why?

Snippet 2 — the resize loop

function fitAll(boxes) {
  for (const box of boxes) {
    const w = box.offsetWidth;     // READ — forces layout
    box.style.width = w + 10 + "px"; // WRITE — invalidates layout
  }
}
Quiz

With 500 boxes this handler is a long task that wrecks INP. What is the defect and the fix?

Snippet 3 — the server/client render

function Price({ cents }) {
  // runs on both server and client during hydration
  const formatted = new Intl.NumberFormat(navigator.language, {
    style: "currency", currency: "USD",
  }).format(cents / 100);
  return <span id="price">{formatted}</span>;
}
Quiz

This component intermittently flashes the price and spikes CLS on hydration. What is the cross-layer bug?

Snippet 4 — the click handler

button.addEventListener("click", () => {
  cart.add(item);
  renderCartBadge();                  // small DOM update
  const report = JSON.parse(hugeBlob); // ~120 ms on a mid-range phone
  analytics.sendSync(report);          // synchronous beacon
});
Quiz

The badge updates but INP for this click is ~180 ms even on an idle thread. What is the fix that respects the INP budget?

Recap

Four snippets, four layers. The event loop drains all microtasks (promise, queueMicrotask) before the next macrotask (setTimeout), so synchronous-then-microtasks-then-macrotask is the order. Interleaving DOM reads and writes forces a reflow per iteration — batch reads then writes. A component that renders a locale- or browser-only value on the server is a hydration mismatch — make the server render deterministic and defer client-only formatting to useEffect. And any synchronous heavy work in a handler before the paint counts against INP — update the UI minimally, then yield. The skill is reading code straight back to the track it stresses.

Continue the climb ↑Putting it together: diagnose and fix a janky page end-to-end
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.