Skip to content

Lesson 2 of 9 · 23m

Rendering, Frames, and Where Your Work Really Runs

How JavaScript interacts with browser rendering, why long tasks wreck input latency, and when to use rAF, idle callbacks, or workers.

JavaScript on the main thread shares time with style calculation, layout, paint, input handling, scrolling compositing handoff, and every callback your framework schedules. A frame budget is roughly 16ms at 60Hz, but your application does not get all 16ms. On real devices, treat anything over ~50ms of uninterrupted JavaScript as a user-visible fault.

The browser does not render between every statement

box.style.width = "300px";
expensiveSyncWork();
box.style.height = "300px";

The user does not see width first and height later. Rendering happens at rendering opportunities between tasks, not because you mutated a DOM property. This is why synchronous work cannot be "made smooth" by touching DOM in smaller chunks unless you also yield back to the event loop.

requestAnimationFrame is for visual work

function animate() {
  box.style.transform = `translateX(${x}px)`;
  if (x < target) requestAnimationFrame(animate);
}
requestAnimationFrame(animate);

requestAnimationFrame runs before the browser paints the next frame. It is the right place for visual mutation because changes line up with the frame lifecycle. It is the wrong place for business logic you merely want to run "soon"; rAF can be throttled or paused in hidden tabs.

Long tasks are the real enemy

A single 180ms task is not "a bit slow"; it delays input, timers, rendering, and promise continuations. Break work into chunks when it must stay on the main thread:

async function processRows(rows) {
  for (let i = 0; i < rows.length; i++) {
    handleRow(rows[i]);
    if (i % 100 === 99) {
      await new Promise(resolve => setTimeout(resolve, 0));
    }
  }
}

The setTimeout yield creates a real task boundary. A promise-only yield often just queues another microtask and does not give rendering a chance.

Offload truly parallel work

Web Workers are for CPU-heavy work that does not need DOM access: parsing large files, compression, image transformations, search indexing. Workers communicate by message passing; structured clone copies most data, while ArrayBuffer values can be transferred without copying.

const worker = new Worker("/assets/indexer.js");
worker.postMessage({ rows }, []);
worker.onmessage = event => renderIndex(event.data);

Field notes

  • Chrome Performance traces label main-thread blocks over 50ms as long tasks; learn to read the flame graph before adding optimizations.
  • requestIdleCallback is not guaranteed to run promptly and is absent from some browser versions. Never put correctness-critical work there.
  • Layout reads (offsetHeight, getBoundingClientRect) interleaved with writes force repeated synchronous layout. Batch reads, then batch writes.
  • If a hidden tab matters, test there. Timer throttling changes behavior in ways local foreground testing hides.

Was this lesson helpful?

Sign in to rate