Rendering PipelineBaseline · Widely

Layout Thrashing (Forced Synchronous Layout)

Mental Model

Reading a geometric property (offsetHeight, getBoundingClientRect()) after invalidating styles forces the engine to run Layout synchronously mid-script instead of deferring it to the end of the frame.

Interactive Simulator

Forced Synchronous Layout Simulator

Vary element count and toggle read-before-write batching.

60 FPS Frame Budget (16.6ms)22.5 ms (50 reflows)
16.6ms
Layout Recalculations (Reflows)
50 / frame
Estimated Main Thread Time
22.5 ms
60 FPS Budget (16.6ms)
EXCEEDED (jank)
Interleaved read/write in loop: 50 forced synchronous layout invalidations (22.5 ms).
Frame Budget (16.6 ms)22.5 ms

Under the Hood

  1. A DOM write (el.style.width = '100px') sets a NeedsLayout dirty flag on the LayoutObject and its ancestors in the Browser Rendering Pipeline.
  2. Normally, geometry computation is deferred until Update the Rendering at the end of the Event Loop frame.
  3. Any box metric read (offsetTop, clientWidth, getComputedStyle(el).width) forces a synchronous Document::UpdateStyleAndLayoutTree() to return an up-to-date value.
JS
// Bad: N forced synchronous layouts in one loop
for (const card of cards) {
  const w = card.offsetWidth; // Read -> forces Layout
  card.style.width = `${w + 10}px`; // Write -> dirties Layout
}

// Good: batch all geometric reads before any DOM write
const widths = cards.map((card) => card.offsetWidth);
cards.forEach((card, i) => {
  card.style.width = `${widths[i] + 10}px`;
});

Problem Genealogy

  • 2005 (jQuery Animations): .each() loops reading .height() then writing .css('height', ...) across entire lists, guaranteeing 10 FPS on a 200-row table.
  • 2013 (FastDOM): Libraries separating read and write queues around requestAnimationFrame using rAF Coalescing.
  • 2020+ (ResizeObserver & content-visibility): Asynchronous dimension observation without forced synchronous reads.

Production Pitfalls

Production Pitfall
  • Virtualized Lists & Grids: Measuring each row after insertion causes O(N) layouts and drops frame rates below 15 FPS.
  • Third-Party Analytics: A tracker calling getBoundingClientRect() right after a framework commit triggers a full-page reflow on someone else's dime.
When Not to Use
  • When not to use manual read/write batching: For resize observation, replace manual metric reads with CSS Container Queries or ResizeObserver instead of maintaining custom read queues.

Connections & Logical Next Steps

Seen in Architecture Autopsies

Mechanical Comparisons