الانتقال إلى المحتوى
FrontendWeb Performance· 7 دقائق للقراءة

Reading offsetHeight Isn't Free: Forced Layout and Layout Thrashing

A geometry read right after a style write makes the browser run layout on the spot, once per read.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A report screen has a sticky header table and a separate body table, and a "Fit columns" button copies each header cell's width onto the cells below. With 300 rows and 6 columns, clicking it froze the page. We ran it in headless Chromium 156: the handler took between 1.9 and 3.2 s across five runs, and a trace showed 1,800 Layout events for one click. In the slowest run, the Long Animation Frames API put 3,139 ms of the handler's 3,250 ms on forced style and layout. Moving the reads out of the loop, with the same writes, brought the handler under 3 ms and down to one layout for the frame.

The common misconception

The belief: reading layout values like offsetHeight is free; only writing styles costs performance.

It sounds right because it's usually true. Read 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

Setting a style doesn't make the browser recompute right away. Normally layout runs once per frame, after your JavaScript and before paint, however many styles changed. Our trace agrees: a hundred writes followed by one read ran exactly one layout.

A geometry read breaks that plan. web.dev explains that at the start of a frame, the layout values from the previous frame are known and available to query. But if your code changed styles since then, those values are stale, and the only way to answer 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

We checked several reads, each placed right after a style write, in a loop of 100. 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

One forced layout is often harmless. is doing it repeatedly: write, read, write, read, so that every read finds the layout invalidated again. In the header table, each iteration wrote a cell width and the next iteration read a header's width, so almost every one of the 1,800 reads forced a fresh layout.

Each of those layouts is a real one. web.dev notes that layout is almost always scoped to the entire document, and that its cost grows with the size of the DOM. In our trace, the report's forced layouts took about a millisecond each: harmless once, painful 1,800 times inside a single task.

Why it shows up as slow clicks

All of this runs on the , inside your event handler, so the browser can't paint or respond to the next input until the loop ends. web.dev ties it directly to responsiveness: to keep INP low, avoid layout when possible. Our three-second click is exactly what a user would feel as a frozen button.

TypeScript columns.ts
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    });  }}

The fixed version reads six widths from a clean layout, which costs nothing, then performs all 1,800 writes. The browser runs layout once, after the handler, as it normally would. The first three rows below come from a simpler test page with 2,000 paragraphs:

Pattern (our runs)LayoutsTime
100 reads on a clean page00.2 ms
100 writes, then one read12 ms
100 times: a write, then a read100145 ms
Header table, write-read loop (1,800 cells)1,8001.9 to 3.2 s
Header table, reads first, same writes1, after the handlerUnder 3 ms

To catch it on real users' devices, the Long Animation Frames API reports frames whose rendering was delayed beyond 50 ms, and each script in them carries a 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:

JavaScript forced-layout.js
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

  1. Record the slow interactionUse 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.
  2. Split reads from writesRestructure 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.
  3. Prefer values you already haveIf 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.
  4. Watch productionSend forcedStyleAndLayoutDuration from 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(), scrollHeight and computed sizes all forced a layout in our test; inline style values 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 forcedStyleAndLayoutDuration in the field.

📚 المصادر

  1. Avoid large, complex layouts and layout thrashing — web.dev
  2. Rendering performance — web.dev
  3. Forced reflow — Chrome DevTools performance insights
  4. Long Animation Frames API — Chrome for Developers
  5. PerformanceScriptTiming: forcedStyleAndLayoutDuration property — MDN

التعليقات (0)