Performance · JavaScript and CSS
Forced reflow
A script on this page changes a style and then reads the page height straight away, 300 times, so the browser lays out the page each time.
How this page is built
The page loads forced-reflow.js. It changes the letter spacing of the main content and then reads the content's height, 300 times in a row. Each read makes the browser lay out the whole page again. The script then puts the spacing back before the browser paints, so nothing on the page moves.
Why it matters
Browsers normally batch style changes and work out the layout once, just before they paint. When a script reads a size or position, such as offsetHeight, straight after changing a style, the browser has to stop and lay out the page there and then. Doing that in a loop repeats the layout work many times and blocks the main thread.
How to check it
Run Lighthouse or PageSpeed Insights on the page and look at the diagnostics. Crawl tools that run Lighthouse for each page, or that measure page resources, report the same finding across the site. Lighthouse names the script that forced the layout and how long the reflows took. In Chrome’s developer tools, the Performance panel marks each forced layout with a warning.
How to fix it
Read all the sizes you need first, then make all the changes, so the browser lays out the page once. Use requestAnimationFrame to group writes, and avoid reading layout properties inside loops that change styles.