Rendering Performance: Beating the 16ms Budget

Rendering Performance: Beating the 16ms Budget in JavaScript: A 60Hz screen redraws every 16.7 milliseconds.

Concept Explanation

A 60Hz screen redraws every 16.7 milliseconds. Everything the browser needs to do for one frame — run your JavaScript, recalculate styles, lay out the page, paint the pixels, composite the layers — has to fit inside that window. Miss it and the frame is dropped. Miss several and the user calls your app "laggy".

The browser's per-frame assembly line is called the rendering pipeline:

JavaScript  ->  Style  ->  Layout  ->  Paint  ->  Composite
                            ^ expensive        ^ cheap

The whole craft of smooth UI is knowing how far down that pipeline your change reaches. Changing width forces the browser to recompute geometry for the element and possibly everything around it: layout, then paint, then composite. Changing transform touches only the last step: the GPU moves an already-painted layer. Same visual result, wildly different cost.

That is why "animate transform and opacity, never top/left/width/height" is not superstition. Those two properties are the only ones that can skip layout and paint entirely.


Syntax or Core Structure

The expensive way and the cheap way

// BAD: layout on every frame
el.style.left = x + 'px';
el.style.width = w + 'px';

// GOOD: compositor-only
el.style.transform = 'translateX(' + x + 'px) scale(' + s + ')';
el.style.opacity = o;

Layout thrashing, and the fix

// BAD: read, write, read, write... forces a synchronous layout each loop
items.forEach((el) => {
  el.style.height = el.offsetHeight * 2 + 'px';
});

// GOOD: batch all reads, then all writes
const heights = items.map((el) => el.offsetHeight);   // read phase
items.forEach((el, i) => {                            // write phase
  el.style.height = heights[i] * 2 + 'px';
});

Animating on the frame boundary

let x = 0;
function step() {
  x += 2;
  box.style.transform = 'translateX(' + x + 'px)';
  if (x < 400) requestAnimationFrame(step);
}
requestAnimationFrame(step);

Telling the browser what not to compute

/* Skip rendering work for off-screen cards entirely */
.card {
  content-visibility: auto;
  contain-intrinsic-size: 0 300px;   /* a size guess so scrollbars behave */
}

/* Promise the browser this element's layout cannot affect anything outside it */
.widget { contain: layout paint; }

Key Features

  • Which properties trigger what: geometry (width, top, margin, font-size) triggers layout; colour and shadow trigger paint; transform and opacity trigger composite only.
  • Reading forces a flush. offsetHeight, getBoundingClientRect(), scrollTop and friends make the browser bring layout up to date right now, discarding its batching.
  • requestAnimationFrame runs your callback just before the next paint, which is exactly where visual updates belong. setInterval(fn, 16) drifts and stutters.
  • content-visibility: auto lets the browser skip layout and paint for off-screen subtrees. On a long page this can be the single biggest win available.
  • will-change promotes an element to its own layer ahead of time. It is a scalpel, not a seasoning.

Real World Example

A table of 5,000 rows that froze on hover. Each row's hover handler read getBoundingClientRect() to position a tooltip, then wrote an inline style, then read again to check for overflow. Read, write, read: three forced synchronous layouts per mouse move, over a large DOM. The Performance panel showed a solid wall of purple "Recalculate Layout" bars.

Two changes fixed it. The reads were hoisted so all measurement happened before any mutation, and the tooltip was positioned with transform rather than top/left. Frame time went from about 40ms to under 4ms, with no change to what the user sees.


Common Mistakes

  • Interleaving reads and writes in a loop. This is layout thrashing, and it is the most common cause of a janky list.
  • Animating top/left/width. Correct-looking, and it runs layout on every single frame.
  • Sprinkling will-change everywhere. Each promoted layer costs GPU memory; too many and you make things slower, especially on mobile.
  • Using setInterval for animation. It has no relationship to the display refresh, so frames land unevenly.
  • Rendering thousands of DOM nodes. Past a few thousand, no amount of CSS tuning beats virtualising the list.
  • Profiling on a desktop only. Enable 4x CPU throttling; a mid-range phone is roughly that much slower.

Best Practices

  • Batch DOM reads, then DOM writes. If a framework offers a scheduler, use it rather than fighting it.
  • Restrict animation to transform and opacity, and let the compositor do the work.
  • Add content-visibility: auto to long lists of cards; it is close to free.
  • Virtualise anything beyond a few hundred rows.
  • Use the Performance panel with CPU throttling on, and read the flame chart rather than guessing. Long purple bars mean layout; long green means paint.
  • Keep animations under 100ms of main-thread work per frame's worth of JS, and prefer CSS transitions where they suffice.

Practice Exercises

  1. The Thrash Test: Write a loop that reads offsetHeight and writes a style on 500 elements, record it in the Performance panel, then split it into read and write phases and compare the flame charts.
  2. The Two Animations: Animate one box with left and an identical box with transform. Turn on "Paint flashing" and "Frame rendering stats" and describe the difference.
  3. The Long Page: Build a page with 2,000 cards, measure the initial render, then add content-visibility: auto with a contain-intrinsic-size and measure again.