🔍 inp.works

Forced synchronous layout: reading geometry after writing to the DOM

What is happening

Normally layout happens after your JavaScript finishes, once per frame. But if you change the DOM and then immediately read a layout property — offsetWidth, scrollHeight, getBoundingClientRect(), getComputedStyle() — the browser must stop and compute layout right there, synchronously, inside your task, because the answer depends on your pending change.

One forced layout is cheap on a small page. On a large DOM, or repeated in a loop (which becomes layout thrashing), it adds hundreds of milliseconds to the handler.

How to recognise it

How to fix it

Problem

grid.style.width = next + "px"            // write: layout is now dirty
const rect = cell.getBoundingClientRect()  // read → forced synchronous layout

Fix

const rect = cell.getBoundingClientRect()  // read first, layout is clean
grid.style.width = next + "px"             // write after; browser lays out
                                           // once, before the next paint
Scan your site for free →
Advertise here