Keeping Forced Layout Out of INP in Core Web Vitals
When JavaScript changes the DOM and then reads an element's height, the browser has to redo layout right away so it can return a correct value. This is a forced synchronous layout (forced reflow). If you repeat this once per element on every keystroke, the Core Web Vitals metric INP (Interaction to Next Paint) gets much worse. On a page with incremental search, where each character typed narrows a list, I tested a 1,000-item list and measured an INP of 688ms. Separating the writes from the reads brought it down to 80–104ms.
The test environment was Ubuntu 24.04, Node.js 22, and web-vitals 5.3.0. I used Chrome for Testing 153, which ships with Playwright 1.63.0, in headless mode, with a viewport of 412px by 823px. The page was served from a server I ran locally at localhost:8080. Each measurement was run twice, and unless noted otherwise, the numbers are from the first run. I did not test the Chrome DevTools Performance panel, CPU throttling, browsers other than Chrome, or real-user data (PageSpeed Insights field data).
What forced layout is
After JavaScript finishes running, the browser computes styles, lays out the page, and paints it. According to web.dev's explanation of layout thrashing, if JavaScript changes a style and then reads a value such as offsetHeight, the browser lays out the page immediately to return a correct value. This is forced synchronous layout (forced reflow).
If you alternate reads and writes many times, each read triggers a forced layout. This is called layout thrashing. The same article says that layout usually applies to the whole document and takes longer as the number of elements grows. The suggested fix is to batch the reads before the writes.
INP measures the time from a click or keypress until the next frame is painted. web.dev's INP explanation treats 200ms or less as "good" and more than 500ms as "poor."
Before the fix: about 1,000 layouts per keystroke
The test page had a search box and a list of 1,000 memos. Each memo's body is limited to two lines with CSS, and only memos that don't fit in two lines show a "more" button. Of the 1,000 memos, 500 are too long to fit.
On each input, the script bolds the search term in each memo's body (write), checks whether the body fits in two lines using scrollHeight and clientHeight (read), and then shows or hides the "more" button (write). I omit the code that builds the list (common.js) and the HTML, and show only the input listener.
box.addEventListener('input', () => {
const q = box.value;
for (const li of items) {
const text = li.firstChild;
text.innerHTML = mark(text.dataset.src, q); // write
const long = text.scrollHeight > text.clientHeight; // read (forced layout happens here)
li.classList.toggle('long', long); // write
}
});
For measurement, I used a script (measure.cjs) that opened the page in Playwright and operated it. I typed "read" into the search box one character at a time, waiting 300ms after each keypress. For each keypress, I recorded:
- The time and breakdown of the keypress (
keydownandkeypress), recorded with Event Timing - The growth of the layout count (
LayoutCount), obtained fromPerformance.getMetricsin the Chrome DevTools Protocol - The script time and the forced layout time within it (
forcedStyleAndLayoutDuration), recorded with the Long Animation Frames API
Finally, I output the INP and its breakdown reported by the attribution build of web-vitals's onINP. I passed durationThreshold: 16 to onINP. According to the web-vitals 5.3.0 README, the default is 40ms, and shorter interactions are not recorded (except the first one). I lowered it so I could compare small values.
$ node measure.cjs interleave batched raf noread
== interleave ==
key "r" value="r": keydown 688ms (delay 0, proc 0, present 688), keypress 688ms (delay 0, proc 672, present 16), layouts 1003, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 670ms (forcedStyleAndLayout 615ms)
key "e" value="re": keydown 400ms (delay 0, proc 0, present 400), keypress 400ms (delay 0, proc 387, present 13), layouts 1001, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 386ms (forcedStyleAndLayout 357ms)
key "a" value="rea": keydown 400ms (delay 0, proc 0, present 400), keypress 400ms (delay 0, proc 385, present 15), layouts 1001, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 384ms (forcedStyleAndLayout 359ms)
key "d" value="read": keydown 392ms (delay 0, proc 0, present 392), keypress 392ms (delay 0, proc 379, present 13), layouts 1001, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 378ms (forcedStyleAndLayout 356ms)
web-vitals onINP: 688ms rating=poor (keyboard, inputDelay 0, processingDuration 687, presentationDelay 1, target #q)
…
Each character typed caused 1,001 to 1,003 layouts. That is nearly the same as the number of memos (1,000), which suggests a layout happened on each per-item read. One keypress took 392 to 688ms, and most of that was the input listener (INPUT#q.oninput). Within that, 356 to 615ms was forced layout. INP was 688ms, rated poor. In the second run, INP was 608ms, also poor.
Batch the writes, then read
Using the idea from web.dev's explanation of batching reads, I split the loop into three parts. Here I need the heights after the writes, so I cannot read first. Instead, I finish all 1,000 writes, then read all the heights together, and finally toggle the "more" button.
box.addEventListener('input', () => {
const q = box.value;
// 1. Finish all the writes first
for (const li of items) {
const text = li.firstChild;
text.innerHTML = mark(text.dataset.src, q);
}
// 2. Read everything together (forced layout happens only once)
const longs = items.map((li) => li.firstChild.scrollHeight > li.firstChild.clientHeight);
// 3. Write using the results of the reads
items.forEach((li, i) => li.classList.toggle('long', longs[i]));
});
== batched ==
key "r" value="r": keydown 104ms (delay 1, proc 0, present 103), keypress 104ms (delay 1, proc 90, present 13), layouts 4, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 75ms (forcedStyleAndLayout 64ms)
key "e" value="re": keydown 72ms (delay 0, proc 0, present 72), keypress 72ms (delay 0, proc 57, present 15), layouts 2, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 56ms (forcedStyleAndLayout 48ms)
key "a" value="rea": keydown 96ms (delay 0, proc 0, present 96), keypress 96ms (delay 0, proc 82, present 14), layouts 2, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 81ms (forcedStyleAndLayout 71ms)
key "d" value="read": keydown 40ms (delay 0, proc 0, present 40), keypress 40ms (delay 0, proc 34, present 6), layouts 2, 「もっと見る」 500件
LoAF scripts: なし
web-vitals onINP: 104ms rating=good (keyboard, inputDelay 1, processingDuration 90, presentationDelay 13, target #q)
The layout count dropped to 2 per character (4 for the first character only). Because no writes happen between the reads, layout is not redone on every read. The number of memos with a "more" button is the same 500 as before. INP went from 688ms to 104ms, rated good. In the second run, it was 80ms.
One forced layout remains, taking 48 to 71ms. Chrome's Forced reflow insight documentation gives 30ms as a guideline: forced layouts longer than that are flagged by the insight.
"LoAF scripts: none" for the fourth character means no record was made. According to Chrome's Long Animation Frames API documentation, only frames whose rendering is delayed by more than 50ms are recorded.
Run the reads after the frame is painted
To remove the one remaining forced layout, I moved the height reads into a task that runs after the frame has been painted. This uses the pattern from web.dev's guide to optimizing INP of calling setTimeout() inside requestAnimationFrame(). The guide presents it as a way to do the updates needed for the next frame first and push the rest to a later task.
let pending = false;
box.addEventListener('input', () => {
const q = box.value;
for (const li of items) {
const text = li.firstChild;
text.innerHTML = mark(text.dataset.src, q);
}
// Run the reads in the next frame, after the frame has been painted
if (pending) return;
pending = true;
requestAnimationFrame(() => setTimeout(() => {
pending = false;
const longs = items.map((li) => li.firstChild.scrollHeight > li.firstChild.clientHeight);
items.forEach((li, i) => li.classList.toggle('long', longs[i]));
}, 0));
});
pending prevents scheduling a second read while one is still waiting to run. With this approach, the "more" button changes in a task that runs after the frame with the bolded search term has been painted.
== raf ==
key "r" value="r": keydown 80ms (delay 0, proc 0, present 80), keypress 80ms (delay 0, proc 64, present 16), layouts 4, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 6ms (forcedStyleAndLayout 0ms)
key "e" value="re": keydown 64ms (delay 0, proc 0, present 64), keypress 64ms (delay 0, proc 49, present 15), layouts 2, 「もっと見る」 500件
LoAF scripts: なし
key "a" value="rea": keydown 80ms (delay 0, proc 0, present 80), keypress 80ms (delay 0, proc 66, present 13), layouts 2, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 6ms (forcedStyleAndLayout 0ms)
key "d" value="read": keydown 64ms (delay 0, proc 0, present 64), keypress 64ms (delay 0, proc 54, present 10), layouts 2, 「もっと見る」 500件
LoAF scripts: INPUT#q.oninput 9ms (forcedStyleAndLayout 0ms)
web-vitals onINP: 80ms rating=good (keyboard, inputDelay 0, processingDuration 64, presentationDelay 16, target #q)
Forced layout inside the listener dropped to 0ms. INP was 80ms, not much different from the batched version. In the second run the order flipped: this version measured 112ms and the batched version 80ms.
For comparison, I also measured a page that never reads heights (so no "more" button is shown). Its INP was 80ms in the first run and 64ms in the second.
== noread ==
…
web-vitals onINP: 80ms rating=good (keyboard, inputDelay 0, processingDuration 67, presentationDelay 13, target #q)
The INP for these three pages fell between 64ms and 112ms. The differences were no larger than the variation between the two runs.
INP for the four approaches

| Approach | Layouts (per character) | Forced layout in listener | INP (first / second run) |
|---|---|---|---|
| Interleave reads and writes (before the fix) | 1,001–1,003 | 356–615ms | 688ms / 608ms |
| Batch the writes, then read | 2–4 | 48–71ms | 104ms / 80ms |
| Run the reads after the frame is painted | 2–4 | 0ms | 80ms / 112ms |
| Don't read heights (for comparison) | 2–3 | 0ms | 80ms / 64ms |
The forced layout values inside the listener are the ones recorded by the Long Animation Frames API in the first run.
What made INP much worse was interleaving reads and writes, which forced a layout once per element. Batching the writes first reduces forced layout to one call. Moving that read to run after the frame is painted removes it from the listener entirely.
Finding forced layout on a production page
The forcedStyleAndLayoutDuration value I used in these measurements is part of the per-script records in the Long Animation Frames API. According to MDN's explanation of forcedStyleAndLayoutDuration, it is the total time that script spent on forced layout and style calculation. web.dev's layout thrashing article also presents it as a way to find forced layout in users' environments. In my test, I collected it with a PerformanceObserver like this (part of measure.cjs):
new PerformanceObserver((l) => {
for (const e of l.getEntries()) for (const s of e.scripts) window.__loaf.push({
invoker: s.invoker, duration: Math.round(s.duration), forced: Math.round(s.forcedStyleAndLayoutDuration),
});
}).observe({ type: 'long-animation-frame', buffered: true });
invoker shows which event listener ran (INPUT#q.oninput in my test). When checking in a local browser, you can also find it in the Forced reflow insight in the Chrome DevTools Performance panel, as described in the web.dev article.





