Rendering Pipeline

Layout Thrashing (Forced Synchronous Layout)

Interleaving DOM writes and geometric reads inside JavaScript forces the engine to recompute Layout synchronously on every iteration.

About this page
READ
2 min
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
Forced Synchronous Layout Simulator

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#

  • 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.

Where this connects

Prerequisites

Next & related

Seen in Architecture Autopsies

Mechanical Comparisons