Skip to the page

How the browser draws the page · senior

Layout thrashing and batching DOM work

Reading a layout value (offsetHeight, getBoundingClientRect) right after a style write forces the browser to do layout immediately; doing read-write-read-write in a loop forces layout every time, so batch all reads, then all writes.

The notes

What you must know

  • Forced synchronous layout: a write marks layout dirty; the next read must compute it now.
  • Fix: read everything first, then write; or schedule writes in requestAnimationFrame.
  • DevTools Performance panel shows purple 'Layout' blocks and a 'Forced reflow' warning.
  • Virtualising long lists and avoiding layout reads in scroll handlers are the big wins.

The red pen

Common traps: mistake → fix

  • Mistake: "reading a property is free"

    Fix: reading geometry after a write can cost a full layout.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is layout thrashing and how do you fix it?
  2. Which properties force layout when read?
  3. How would you find it in DevTools?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview