日本語

Finding Slow Code with the Chrome DevTools Performance Panel

​​Chrome DevTools​Chrome,Core Web Vitals,INP,JavaScript

When there is a lag between pressing a button and the screen updating, recording the interaction in the Chrome DevTools Performance panel can pinpoint which function and which line are using the time. This method is useful for finding the cause when a page has a poor INP (Interaction to Next Paint) value or responds slowly to clicks. I recorded a deliberately slow page, followed the Long task display through the Bottom-up tab to the Sources panel to find the offending line, then fixed it and recorded again to compare.

Note

The test environment was Ubuntu 24.04 with Google Chrome for Testing 153.0.8010.12. Because this is a headless test environment without a screen, I opened Chrome on a virtual display (Xvfb). Instead of operating DevTools by hand, I used Playwright and xdotool to click and drag, and took the screenshots from there. The DevTools UI is in English. The numbers are from one recording each, and they change a little every time you record. I did not test CPU throttling, Record and reload, builds with source maps, or a browser with my usual Chrome extensions installed.

Preparing a slow page

The test page was the following page.html. When the sort button (its label is Japanese for "sort") is pressed, it creates 8,000 items, sorts them with a bubble sort, and displays the first value. Bubble sort slows down sharply as the number of items grows, so the click handler becomes heavy.

html
<!doctype html>
<html lang="ja">
<head><meta charset="utf-8"><title>遅いボタン</title><link rel="icon" href="data:,"></head>
<body>
<button id="sort">並べ替える</button>
<p id="out">まだ押していません</p>
<script>
function makeData(n) {
  const a = [];
  for (let i = 0; i < n; i++) a.push({ id: i, score: Math.random() });
  return a;
}
function slowSort(items) {
  // Intentionally slow sort (bubble sort)
  const a = items.slice();
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < a.length - 1 - i; j++) {
      if (a[j].score > a[j + 1].score) { const t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
    }
  }
  return a;
}
function render(items) {
  document.getElementById('out').textContent = '先頭のスコア: ' + items[0].score.toFixed(4);
}
document.getElementById('sort').addEventListener('click', () => {
  const data = makeData(8000);
  const sorted = slowSort(data);
  render(sorted);
});
</script>
</body>
</html>

I served this page with python3 -m http.server 8080 and opened http://localhost:8080/page.html in Chrome.

Recording only while you interact

The official reference for the Performance panel, on recording a running page, describes these steps:

  1. Open the Performance tab in DevTools
  2. Press the Record button (in the test environment, the round button at the top left of the Performance tab)
  3. Interact with the page
  4. Press the Record button again, or the Stop button that appears, to stop

In the test, I pressed Record, clicked the Sort button once, waited about one second, and pressed Stop. After stopping, the following screen appeared.

The Performance panel after stopping the recording. The Insights panel on the left shows INP 220ms in orange, and the Main row has a Task with red diagonal hatching
Right after recording. INP 220ms appears at the top left, and a Task with red diagonal hatching appears in Main

The Insights panel on the left shows the INP measured in this recording as 220 ms. In the CPU graph of the overview bar at the top, a yellow peak appears at the time of the click. The Interactions row has one click (Pointer), and below it, in the Main row, there is a Task whose right side is shown with red diagonal hatching.

Clicking the hatched Task to confirm the Long task

Dragging across the overview bar at the top zooms in on that range. After selecting a range that includes the click and zooming in, I clicked the Task in the Main row, and detailed information appeared in the Summary tab below.

The Task selected. The Summary shows the Long task warning "Warning Long task took 212.54 ms" and Duration 212.54 ms
Selecting a Task shows a Long task warning and its duration in the Summary
text
Task
Warning Long task took 212.54 ms.
Duration 212.54 ms (self 0.16 ms)

According to the official reference, a Long task gets a red triangle, and the part beyond 50 ms is painted red. In the test environment, this part showed as red diagonal hatching. This Task took about 213 ms, so the part after the first 50 ms is hatched. In the Interactions row, the click is painted red in the same way for the part beyond 200 ms.

Below the Task are the functions that were called within it. From top to bottom, they are stacked in call order: Event: click, Function call, (anonymous) (the anonymous function passed as the click event listener), and slowSort. When I clicked slowSort at the bottom, the following appeared:

text
slowSort
Duration 202.52 ms
Source page.html:13:18

This shows that about 203 ms of the 213 ms Task was spent inside slowSort.

Ordering the functions by time with the Bottom-up tab

On a page with deep call stacks, it is hard to follow stacked calls by eye. Instead, use the Bottom-up tab next to Summary. According to the official reference, the Bottom-up tab shows the functions in the selected range sorted by the total time they use directly. Self time is the time spent directly in that function, and Total time is the time including the functions it calls.

The Bottom-up tab. The top row of the Self time column is slowSort at 203.5 ms, 96.0%
Bottom-up tab. Sorted by Self time, slowSort comes first

With the Task selected, I opened the Bottom-up tab, and the entries appeared in this order (by Self time, largest first):

Self time Total time Function
203.5 ms (96.0%) 205.2 ms slowSort (page.html:13:18)
4.0 ms (1.9%) 4.0 ms Minor GC
1.5 ms (0.7%) 210.9 ms (anonymous) (page.html:26:59)
1.5 ms (0.7%) 3.3 ms makeData (page.html:8:18)

(anonymous) has a large Total time of 210.9 ms, but its Self time is only 1.5 ms. The time is spent in the functions it calls, not in its own code. When looking for what to fix, look at slowSort, which has the largest Self time.

Using the INP breakdown to split the delay

Opening the INP breakdown in the Insights panel on the left split the time from the click to the next displayed frame into three parts.

The INP breakdown opened. Input delay 1 ms, Processing duration 213 ms, Presentation delay 5 ms
INP breakdown. Most of the click delay was Processing duration
Phase Time
Input delay (from the click until the event listener starts running) 1 ms
Processing duration (running the event listener) 213 ms
Presentation delay (from when the result is ready until the browser displays the next frame) 5 ms

These three phases correspond to the three stages of an interaction described in web.dev's article on improving INP. In this recording, almost all of the time is Processing duration, so making the code in the event listener lighter should lower the INP. According to the same article, Input delay can grow when other work is running on the main thread, such as script loading or timer callbacks.

Looking at the exact lines in the Sources panel

Clicking the page.html:13:18 link in the Bottom-up tab opened page.html in the Sources panel at line 13, where slowSort is defined. The official reference also explains that this link opens the source file in the Sources panel.

The Sources panel. Next to the line numbers, the recorded time is shown, and line 18 shows 174.3 ms
Sources panel. The recorded time appears next to the line numbers, and line 18 was 174.3 ms

In the test environment, when I opened the Sources panel after recording in the Performance panel, the time spent on each line appeared next to the line number. The numbers were:

Line Code Time
16 Outer for 12.5 ms
17 Inner for 17.7 ms
18 The if that compares and swaps 174.3 ms
28 Call to slowSort(data) 1.2 ms

Line 18, which runs on every iteration of the inner loop of the nested loops, accounts for most of the time. At this point, it is clear that the sorting method itself should be changed.

Recording again after the fix to compare

I replaced slowSort with fastSort, which uses the built-in Array.prototype.sort. Only the following part changed:

js
function fastSort(items) {
  // Use the built-in sort
  return items.slice().sort((x, y) => x.score - y.score);
}
js

const sorted = fastSort(data);

When I recorded again using the same steps, the INP was 15 ms.

The screen after the fix. INP is 15ms in green, and the INP breakdown shows Input delay 1 ms, Processing duration 8 ms, Presentation delay 6 ms
Recording after the fix. INP 15ms, Processing duration 8 ms
Before the fix After the fix
INP 220 ms 15 ms
Input delay 1 ms 1 ms
Processing duration 213 ms 8 ms
Presentation delay 5 ms 6 ms

In the Bottom-up tab after the fix, the Total time of fastSort, the sorting function, was 4.2 ms. The Task with red diagonal hatching was also gone. Recording the same interaction before and after the fix and comparing the numbers lets you confirm the effect of the fix directly.

​Posted by 86