Skip to content
Back to Concepts
CSS UniverseTrack 9: Engine InternalsReflow & Repaint
Engine Internals ยท Track 9Core Concept 35

Reflow, Repaint & Layout Thrashing

The secret cause of laggy websites. Discover why Repaint is fast but Reflow is expensive, understand the lethal performance killer known as Layout Thrashing (Forced Synchronous Layout), and learn how batching DOM reads and writes saves 60fps frame rates.

1. Mental Model: Knocking Down Walls (Reflow) vs. Repainting Walls (Repaint)

Reflow (Layout): Heavy Structural Renovation

Knocking down a structural wall in a house. The roof settles, plumbing pipes must be rerouted, adjacent rooms shrink, and all furniture must be repositioned. Triggered by changing width, height, font-size, or adding/removing DOM elements.

Repaint: Changing Wall Paint Color

Painting your bedroom wall from yellow to blue. Zero walls move; furniture remains exactly where it was. Triggered by changing color, background-color, or visibility. Much faster than reflow!

2. The Deadly Crime: Layout Thrashing (Forced Synchronous Layout)

Browsers normally batch DOM writes and execute them once at the end of the 16.6ms frame. However, if you write a style and then immediately ask for a geometric dimension in JavaScript, you force the browser to execute an emergency synchronous reflow on the spot!

โŒ Layout Thrashing (Stutters at 5fps)
cards.forEach(card => {
// WRITE (invalidates layout):
card.style.width = '200px';
// READ (forces immediate emergency reflow!):
const h = card.offsetHeight;
});
โœ… Batched Execution (Silky 60fps)
// 1. Read all heights first (Zero reflow):
const heights = cards.map(c => c.offsetHeight);
// 2. Write all widths together (1 single reflow):
cards.forEach(card => card.style.width = '200px');

3. Interactive Studio: Simulate Layout Thrashing vs. Batched Loop

Run Benchmark Script
Simulated Frame Budget
Calculated Frame Rate:60 FPS
โœ“ Batched writes completed within the 16.6ms frame budget.
DOM Element Batch ProgressIdle
#card-node-1Ready
#card-node-2Ready
#card-node-3Ready
#card-node-4Ready

First-Year Student Traps: Reflow & Repaint

  • 1.Properties that Trigger Forced Reflow: Accessing any of these properties right after writing style changes will force an emergency reflow: offsetWidth, offsetHeight, scrollTop, getBoundingClientRect(), and getComputedStyle().
  • 2.FastDOM / requestAnimationFrame: If you must mutate DOM styles based on measurements, wrap reads in the current tick and wrap writes inside requestAnimationFrame(() => { ... }) to let the browser execute them at the start of the next refresh cycle.
Next Masterclass

GPU Acceleration & will-change

Promote elements into independent GPU compositing layers without causing memory leaks.

Continue to Concept 36