layout thrashing

Slowing a page by repeatedly changing styles and then reading sizes, forcing the browser to recalculate layout again and again.

In more detail

JavaScript positioning code that measures and moves elements on every scroll can cause it. Native CSS anchor positioning avoids it by letting the browser position elements in its normal layout pass.

Example

In the browser: The fix is to batch: read all the measurements you need first, then make all the writes. requestAnimationFrame is a natural place to group writes for the next frame.

Related terms

Taught in

References