Frontend / Reflow vs repaint

Reflow vs repaint

Some page changes make the browser re-measure every box; others only need new pixels. See which is which, what each costs, and how one loop can trigger layout again and again.

Intermediate7 steps~7 min
Layout — measure and place every box
Paint — fill in the pixels
Read — script asks for a size or position
Write — script changes a style
// THE SHORT VERSION

In 30 seconds

A browser turns a page into pixels in stages: work out styles, compute each box’s size and position (layout), fill in pixels (paint), then assemble the result.

Change something’s size or position and layout must run again: a reflow, the expensive case. Change only how it looks, such as a colour, and the browser can skip layout and just repaint.

Scripts make this worse when they change a style and then immediately ask for a measurement, forcing layout on the spot. Doing all the reads first and the writes afterwards avoids it.

Reflow vs repaint, step by step

STEPS01 / 07

Simplified pipeline: real browsers have more stages, and engines differ on exactly which properties trigger what. Modern layout is incremental, so a reflow rarely re-measures the whole page.

STEP 01 / 07 From styles to pixels
THE MATH
style > layout > paint > composite 1 frame at 60 Hz = 16.7 ms
  1. Step 1: From styles to pixels. To put a page on screen, a browser works through a pipeline. Style decides which CSS applies to each element. Layout computes every box’s size and position. Paint fills in the pixels. Composite assembles the painted layers. To look smooth, all of it must fit in one frame: about 16.7 ms at 60 frames a second.
  2. Step 2: Reflow: geometry changed. Make box B 80px wider and C has to move over to make room. Any change to an element’s size or position can affect its neighbours, parents and children, so the browser must run layout again, then repaint and composite. Recomputing layout is called reflow.
  3. Step 3: Repaint: only looks changed. Now change only B’s background colour. Nothing gets bigger, smaller or moves, so the positions the browser already worked out are still correct. It skips layout and only repaints the pixels that look different. A repaint without a reflow is the cheaper kind of update.
  4. Step 4: What each change costs. The further left a change enters the pipeline, the more work follows. Layout is usually the costly stage, because one changed box can move many others and each must be measured again. Painting touches only the changed area. A few properties, such as transform and opacity, can skip both and go straight to compositing.
  5. Step 5: A read can force layout. Browsers are lazy on purpose. Writing a style only marks the layout as out of date; the real work waits until the frame is drawn, so many writes cost one layout. But if a script asks for a size or position after a write, the browser must run layout at once to give a correct answer. This is a forced synchronous layout.
  6. Step 6: Layout thrashing. Put a read and a write in the same loop and the problem multiplies. Each pass reads a width, then writes a style. The write makes the layout stale, so the read in the next pass forces a fresh layout. Four items cost four layouts in a single frame; a thousand would cost a thousand. This is layout thrashing.
  7. Step 7: Batch reads, then writes. The fix is to separate the two. Do every read first, while the layout is still fresh, and keep the answers in variables. Then do all the writes. No read follows a write, so nothing is forced, and the browser does one layout when it draws the frame. The page ends up looking the same, with a fraction of the work.
VALUES SO FAR
frame16.7 ms @ 60Hzbudget
width····reflow
color····repaint
opacity····cheapest
read····sync
loop····thrash
batched····fix

Spotted a mistake on this page?