日本語

Chrome DevTools “[Violation]” Messages: Meaning and How to Find the Cause

​​Chrome DevTools​JavaScript,Performance,Troubleshooting

Lines that start with [Violation] in the Chrome DevTools console are not errors. The browser detects work that slows down how quickly the page responds after a click or other input, and logs it. Each message says what ran and how many milliseconds it took, so it gives you a starting point for finding the cause of a sluggish page.

I built a page that produces [Violation] messages on purpose, quoted the messages exactly as they appeared, showed how to read them and how to find the code responsible, and confirmed that the messages stop appearing after the fix.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, and Chromium 158 (the build bundled with Playwright 1.63.0). I served the page with http-server 14.1.1. The DevTools screenshots were taken with a headed Chromium running on a virtual display (Xvfb). To confirm the message strings, I had the browser emit violation logs with the same settings as DevTools and collected them through the CDP (Chrome DevTools Protocol). For the Verbose display in the screenshots, I changed the DevTools setting by writing it into the browser's settings file instead of operating the menu. I did not test browsers other than Chromium, real smartphones, or investigation through the Performance panel.

Building a Page That Produces [Violation]

I prepared a page with a list of 1,000 rows and two buttons. The "Sort" button runs a time-consuming calculation directly inside its click listener. The "Match heights" button reads and then writes the height of each row in the list. I wrote the JavaScript in bad.js.

js
// Before the fix: time-consuming work runs inside the click, and heights are read and written alternately
const list = document.getElementById('list');
for (let i = 0; i < 1000; i++) {
  const div = document.createElement('div');
  div.className = 'cat';
  div.textContent = `猫 ${i + 1}`;
  list.append(div);
}

// 1. Run the time-consuming work directly inside the click listener
document.getElementById('sort').addEventListener('click', () => {
  const start = performance.now();
  while (performance.now() - start < 300) {} // stands in for a 300ms calculation
  console.log('並べ替えました');
});

// 2. Read and write heights in turn (each read forces a layout calculation)
document.getElementById('resize').addEventListener('click', () => {
  for (const cat of list.querySelectorAll('.cat')) {
    cat.style.height = `${cat.offsetHeight + 1}px`;
  }
});

I opened this page with DevTools open, then pressed "Sort" and "Match heights" once each.

With the Default Settings, [Violation] Is Hidden

After the presses, the console showed only one line: the console.log output "Sorted". The toolbar on the right displayed "3 hidden", meaning three log entries were hidden.

The DevTools console shows only one line, the Sort button's console.log output "Sorted". The Log level button in the toolbar is set to Default levels, and "3 hidden" appears on the right
The console with the default settings. The three [Violation] entries are hidden

[Violation] logs are emitted at the Verbose level. In the logs I collected through CDP, the violation messages arriving from the browser were also at the verbose level. DevTools adds the [Violation] prefix when it displays them (ConsoleViewMessage.ts in the DevTools source). With the default setting (Default levels), Verbose is not checked, so they stay hidden.

To display them, click the "Default levels" button in the toolbar and check Verbose. The official documentation on logging to the console also includes an example in which, with Verbose checked, the operation produces [Violation] 'click' handler took 3000ms. I cover filtering logs by level in Filtering console logs by type and text in Chrome DevTools.

The Messages That Appeared and What They Mean

With Verbose checked and the same operations repeated, these three lines appeared on a yellow background.

text
[Violation] 'click' handler took 300ms
[Violation] 'click' handler took 369ms
[Violation] Forced reflow while executing JavaScript took 355ms
The DevTools console shows four lines: Sorted (Japanese label), [Violation] 'click' handler took 300ms, [Violation] 'click' handler took 369ms, and [Violation] Forced reflow while executing JavaScript took 355ms. At the right end of the two upper Violation lines are links to bad.js:11 and bad.js:18
The console with Verbose displayed. The three [Violation] lines appear on a yellow background

The first line, 'click' handler took 300ms, means that running the click event listener took 300 milliseconds. The "Sort" listener runs a 300ms calculation as-is, so it took 300 ms every time. The official documentation's example for 'click' handler took 3000ms also explains that the page stops responding for several seconds during such an operation.

The second and third lines came from pressing "Match heights". Forced reflow while executing JavaScript took 355ms means that, while JavaScript was running, the browser had to recalculate the layout on the spot, and that recalculation took 355 milliseconds. web.dev explains this behavior as forced synchronous layout. Most of the listener's time (369 ms) in the second line was this layout calculation.

The millisecond values changed a little between runs of the same operation. Across three collections through CDP, the 'click' handler for "Match heights" ranged from 291 to 357 ms, and Forced reflow ranged from 282 to 345 ms.

Finding the Cause from the Link at the Right End of the Line

At the right end of the 'click' handler lines, links to bad.js:11 and bad.js:18 appeared. Line 11 of bad.js is the addEventListener line for "Sort", and line 18 is the one for "Match heights". Looking at the line each link points to tells you which listener is slow.

The Forced reflow line had no link. Starting from the link on the 'click' handler line logged in the same operation (bad.js:18), you can find which listener triggered the layout calculation.

The Threshold for When [Violation] Is Logged

[Violation] is logged when a task exceeds a set number of milliseconds. The browser receives these thresholds from DevTools. The DevTools source (LogModel.ts) mainly defines the following values.

Type (name in CDP) Threshold
Event listener (handler) 150ms
Timer and requestAnimationFrame callbacks (recurringHandler) 50ms
Layout calculation during JavaScript (longLayout) 30ms
Long task (longTask) 200ms

In my tests, a click listener did not produce a message at 100 ms but did produce 'click' handler took 159ms at 160 ms. setTimeout produced 'setTimeout' handler took 100ms even at 100 ms, which is below the click threshold of 150 ms. In the Chromium source code, setTimeout and setInterval and requestAnimationFrame are judged against the recurringHandler threshold (50 ms), while event listeners are judged against the handler threshold (150 ms). requestAnimationFrame and setInterval at 80 ms produced 'requestAnimationFrame' handler took 80ms and 'setInterval' handler took 80ms. The part before "handler" shows the name of the event or timer.

Moving Time-Consuming Work to a Worker

I moved the 300 ms calculation of "Sort" to a Web Worker. A Worker runs on a thread separate from the page, so even if the calculation takes time, the click listener finishes right away.

I wrote the work the Worker performs in sort-worker.js.

js
// Inside the Worker, long-running work does not block user interaction on the page
self.addEventListener('message', () => {
  const start = performance.now();
  while (performance.now() - start < 300) {} // stands in for a 300ms calculation
  self.postMessage('done');
});
js
// The "Sort" part of good.js (after the fix)
const worker = new Worker('sort-worker.js');
worker.addEventListener('message', () => console.log('並べ替えました'));
document.getElementById('sort').addEventListener('click', () => {
  worker.postMessage('sort');
});

The click listener now only asks the Worker to do the work with postMessage. When the calculation finishes, the message from the Worker triggers the "Sorted" log.

Read All Heights First, Then Write All of Them

"Match heights" used to repeat "read the height, then write it" for each row. After a write, reading offsetHeight on the next row makes the browser recalculate the layout right away to reflect that write. This was repeated for 1,000 rows. The web.dev explanation of forcing layout repeatedly (layout thrashing) also uses a loop that alternates reads and writes as its example. For how this pattern affects interaction responsiveness (INP), see How to Keep Core Web Vitals INP Clear of Forced Layout.

The fix is to read the heights of all rows first, then write them all at once.

js
// The "Match heights" part of good.js (after the fix)
document.getElementById('resize').addEventListener('click', () => {
  const cats = [...list.querySelectorAll('.cat')];
  const heights = cats.map((cat) => cat.offsetHeight);
  cats.forEach((cat, i) => {
    cat.style.height = `${heights[i] + 1}px`;
  });
});

After the Fix, [Violation] No Longer Appears

On the fixed page, with Verbose still checked, the same operations produced only one line in the console, "Sorted", and no [Violation] at all.

The DevTools console shows only one line, Sorted (Japanese label). The Log level button is set to All levels, and a link to good.js:12 appears at the right end
The fixed page. No [Violation] appears even with Verbose displayed

Across three collections through CDP, there were zero [Violation] messages. I then lowered the threshold so that listeners and layout calculations would be reported from 1 ms. Only 'click' handler took 1ms for "Match heights" appeared, and no Forced reflow was logged. The "Sort" listener finished in under 1 ms and produced nothing.

Point

[Violation] is not visible unless Verbose is displayed. When investigating a sluggish page, check Verbose, perform the operation, and then use the link on the 'click' handler took line to find the slow listener.

​Posted by 86