Full answer
The browser does not recalculate layout after every style change. It waits and does the work once, before the next paint. But some JavaScript reads force it to calculate the layout at once, so the answer is correct. Examples: offsetHeight, offsetWidth, getBoundingClientRect(), scrollTop and getComputedStyle(el).height.
Layout thrashing happens when a loop mixes reads and writes. A write (such as el.style.width = ...) makes the layout dirty. The next read forces a full layout. If this repeats for 100 elements, the browser does 100 layouts instead of one. On a large page, each layout is expensive.
Fixes:
- Batch. First read everything you need into variables, then write everything. The browser does one layout for the reads, and one for the paint.
- Avoid the reads. Use CSS (Flexbox, Grid,
aspect-ratio) to size things, so no script needs to measure. - Schedule writes with
requestAnimationFrame, so they run once per frame. - Animate
transformandopacity, which do not need layout. - Use
ResizeObserverorIntersectionObserverinstead of reading sizes in scroll handlers.
You can find the problem in the performance panel. Look for purple "Layout" blocks, often marked as forced reflow, with a warning and the line of code that caused them.
Both functions do the same visible work. The first reads and writes in turns, so each read forces a layout. The second reads all widths first and then writes them. In Chrome, thrash() caused about as many layouts as there are boxes, and batched() caused only a few.
Measure on a real page, because the cost depends on how big and complex the layout is.
<div id="host"></div>
<script>
for (let i = 0; i < 40; i++) { const d = document.createElement("div"); d.className = "box"; d.textContent = "x"; host.append(d); }
const boxes = [...document.querySelectorAll(".box")];
function thrash() {
boxes.forEach(b => { b.style.width = (b.offsetWidth + 1) + "px"; });
}
function batched() {
const widths = boxes.map(b => b.offsetWidth);
boxes.forEach((b, i) => { b.style.width = (widths[i] + 1) + "px"; });
}
</script>The layout counts were read from the Chrome DevTools protocol while each function ran, so the numbers come from the browser and not from timing. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Reads like offsetHeight force a synchronous layout.
- Mixing reads and writes in a loop repeats the layout.
- Batch reads, then writes.
- Prefer CSS sizing over measuring.
- Use the performance panel to find forced reflows.
Common mistakes
- Reading offsetHeight or similar properties inside a loop that also writes styles.
- Using scroll handlers that measure many elements on every event.
- Guessing at the cause without looking at the performance panel.
Follow-ups
- Which properties force layout when read?
- How does requestAnimationFrame help?
Tips
- Define it as a loop of write, read, write, read, and say why a read forces layout.
- Give the batching fix first, then say that CSS often removes the need to measure.