
Reading offsetHeight Isn't Free: Forced Layout and Layout Thrashing
- كاتب موثّق
محتويات المقال
The common misconception
offsetHeight is free; only writing styles costs performance.
offsetHeight a hundred times in a row and nothing happens: in our trace, a hundred reads on a clean page ran zero layouts. A getter looks like a plain property access, and writes are where the visible change happens, so writes get the blame. When a profile does point at the read, it looks like a getter that happened to be slow, not like a cost your own writes created.
What actually happens
Writes are deferred, reads can't be
offsetHeight correctly is to run style and layout now, synchronously, in the middle of your function. web.dev calls this a , or forced reflow, and Chrome's DevTools defines it as JavaScript querying geometric properties such as offsetWidth after styles were invalidated.
Which reads force it
offsetHeight, offsetWidth, clientWidth, scrollHeight, getBoundingClientRect() and getComputedStyle(el).width each forced a layout on every pass. getComputedStyle(el).color forced style recalculations but no layouts, since a color doesn't depend on geometry. Reading el.style.width or el.className forced neither: they return what you set, not what was computed.
Thrashing: one layout per loop iteration
Why it shows up as slow clicks
function syncColumnWidths( headers: HTMLTableCellElement[], rows: HTMLTableRowElement[],) { for (const row of rows) { headers.forEach((th, i) => { row.cells[i].style.width = `${th.offsetWidth}px`; // read after a write }); }}function syncColumnWidths( headers: HTMLTableCellElement[], rows: HTMLTableRowElement[],) { const widths = headers.map((th) => th.offsetWidth); // all reads first for (const row of rows) { widths.forEach((w, i) => { row.cells[i].style.width = `${w}px`; // then only writes }); }}forcedStyleAndLayoutDuration. Chrome supports it from version 123; MDN still marks it experimental. This observer flagged our handler, BUTTON#b.onclick, and stayed silent for the fixed version:
new PerformanceObserver((list) => { for (const frame of list.getEntries()) { for (const script of frame.scripts) { if (script.forcedStyleAndLayoutDuration > 30) { console.warn(script.invoker, script.forcedStyleAndLayoutDuration); } } }}).observe({ type: "long-animation-frame", buffered: true });How to apply it in your project
Record the slow interaction Use the Performance panel in DevTools and check the Forced reflow insight. Chrome's guidance is to have no forced reflow longer than 30 ms; follow it to the function that reads. Split reads from writes Restructure the code into a measure phase that collects every value it needs, then a mutate phase that only writes. In loops, hoist reads out of the loop when the value doesn't change. Prefer values you already have If you just set a width, reuse the number instead of reading it back. For sizes that change on resize, compute them once per resize, not once per element. Watch production Send forcedStyleAndLayoutDurationfrom long animation frames to your monitoring, so regressions show up as data, not as user complaints.
Key takeaways
Style writes are deferred and cheap; a geometry read after a write forces style and layout to run immediately. offsetHeight,getBoundingClientRect(),scrollHeightand computed sizes all forced a layout in our test; inlinestylevalues didn't.Layout thrashing is a loop of write-then-read, paying for one full layout per iteration. Batch reads first and writes second, so the browser runs layout once per frame. Find it with the DevTools Forced reflow insight in the lab and forcedStyleAndLayoutDurationin the field.
التعليقات (0)