Reflow vs repaint: the whole lesson in words
A grid of 300 cards freezes for a moment every time the window resizes, on a mid-range phone.
Symptom
An agent made a grid of 300 order cards all the same height. On a mid-range phone, the page freezes for a moment every time it resizes.
Code that looks cheap can freeze the page.
Predict
Before we explain anything: the loop writes a height, then reads a height, for every card. How many times does the browser run layout?
A write marks it stale
Setting a style is a write. The browser does not lay out right away. It only marks layout as out of date, and the script keeps going.
A write only marks layout as out of date.
Layout once, at the end
When the script's task ends, the browser lays out once for all the writes, then paints and composites. This is the cheap, normal path.
Normally, layout runs once, after the script.
A read forces layout
Reading offsetHeight is different. To give a correct answer, the browser must lay out right now, in the middle of the script. This is a forced synchronous layout.
Reading a size after a write forces layout immediately.
Thrashing
In the agent's loop, every write is followed by a read, so every read forces another layout. Chrome measured one call: layout ran 301 times, and the task took 191.9 milliseconds. One frame is only 16.7 milliseconds.
Write, read, write, read: layout on every turn. This is layout thrashing.
Read first, then write
Now change the order. First write every height, then read every height, then write the result. How many layouts?
Batched
The first read forces one layout, and the other reads reuse it. The final writes are laid out once at the end. Measured: 2 layouts, and the task took 9.7 milliseconds instead of 191.9.
Batch reads together and writes together.
Break it: colour only
Now the loop changes only each card's colour and reads nothing. No box moves, so there is no layout at all, only a repaint. Measured: 0 layouts, and the task took 1.2 milliseconds.
Repaint without reflow is the cheaper path.
Agent bug
Back to the agent's loop. The write and the read sit side by side, so the layout runs once per card, on every resize. Read all heights first, then write. Or let CSS grid make the rows equal, and drop the script.
Never read a size right after writing a style, inside a loop.
Recap
A write marks layout out of date. A read of a size forces layout now. Mix them in a loop and layout runs every turn. Read first, write after, and prefer changes that only repaint.
Read first, then write.
The common wrong idea
Layout happens once per frame, whatever my script does. Reading a size like offsetHeight is free: it is just a property.
What really happens
A style write only marks layout as out of date. Reading a size such as offsetHeight or getBoundingClientRect() after a write forces the browser to lay out right then. Alternate writes and reads in a loop and layout runs on every turn. Read everything first, then write.
The one-page cheatsheet for this lesson →
Sources
- web.dev/articles/avoid-large-complex-layouts-and-layout-thrashing
- gist.github.com/paulirish/5d52fb081b3570c81e3a
- developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight
- developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
- drafts.csswg.org/cssom-view/
- developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-rows
- developer.mozilla.org/en-US/docs/Web/API/ResizeObserver