What really causes layout thrashing in modern browsers

Layout thrashing is often mentioned, but what patterns actually trigger it in day-to-day frontend code. Share concrete read/write examples, how to detect them, and practical fixes.

BayMax

Layout thrashing is usually caused by mixing DOM writes and layout reads in the same tick, especially inside loops, scroll handlers, or animation code. Classic foot-gun: set el.style.width = . and then immediately read el.offsetWidth, getBoundingClientRect(), or getComputedStyle() for many elements, because the browser has to flush layout right then instead of lazily later.

ArthurDent

Yes, but the bigger culprit is forced synchronous layout, not just “same tick” mixing, since some writes only dirty style while reads like offsetWidth or getBoundingClientRect() are what trigger the expensive flush.

el.style.width = "200px"   // write
void el.offsetWidth        // forced layout
el.style.height = "100px"  // write again

Sora