Performance Profiling — Debugging

Record the slow action in the DevTools Performance panel (Ctrl+E), preferably with CPU throttling (the CPU slowed down on purpose to be more like a phone). In…

A user says your page feels "heavy" every time the filter button is clicked. Heavy where: fetching the data, building the list, or redrawing the screen? Instead of guessing, record everything the browser does, millisecond by millisecond, until you can see exactly what makes the page slow. This way of measuring is called performance profiling.

In a recording of the browser's work, two numbers are important. Think of a project manager. The whole project takes 60 days, but the work the manager does personally is only 5 days. The rest is done by the team. If you want the project to go faster, pushing the manager is pointless: find the team member whose work takes longest. In DevTools, each function is like that manager, and the functions it calls are like the team. The flame chart (a chart of stacked bars in a DevTools recording) shows two numbers for every function. The first is its total time, like the project's 60 days. The second is the time the function spends on its own work, like the manager's 5 days.

Recording in the Performance panel

  1. Open DevTools and pick the Performance panel. Its start page already shows LCP, CLS and INP from your interactions, live. These three are page speed numbers: how fast the largest content appears (LCP), how much the layout shifts (CLS), and how fast the page responds to a click or key press (INP).
  2. Click Record (Ctrl+E; on a Mac Cmd+E), do the action that feels slow (click, scroll, type), then click Stop. To measure the page load, use Record and reload (Ctrl+Shift+E).
  3. DevTools shows the recording. The Main track (a row of the recording) is the main thread (the page's main line of work), where JavaScript, layout (calculating the position and size of elements) and paint (drawing the screen) take turns. The main thread can only do one thing at a time.

A developer's laptop is far faster than most users' phones. Before recording, set CPU throttling (slowing the CPU down on purpose) to 4x slowdown. Also record in an incognito window, so extensions don't appear in the recording. That 4x is relative to your laptop. To get closer to a real phone, choose Calibrate in the CPU menu (Capture settings). DevTools measures your laptop, then makes presets (ready-made settings) for low-tier and mid-tier phones.

After recording, open the Insights tab in the left sidebar of the Performance panel. There DevTools lists the problems it found in the recording, such as a breakdown of the LCP and INP times, or third-party code (code from other sites, such as ads). Click an item, and the matching part of the recording is highlighted. This tab is a good place to start before you read the flame chart yourself.

Reading a flame chart

The Lab in this lesson records real code. There is one task (one piece of work the browser runs without a break) that calls several functions. Those functions are made to stay busy on purpose for a set time. Then there is a second task called paint. The Lab notes when each function starts and ends with performance.now(). The result is six samples (each the record of one function) like the exercise's, although a real recording can be off by a fraction of a millisecond. The Parent column is the function that called it:

Sample Start End Parent
task 0 ms 60 ms (top level)
parseHTML 0 ms 10 ms task
script 10 ms 55 ms task
fetch 10 ms 30 ms script
render 30 ms 55 ms script
paint 60 ms 70 ms (top level)

These are the same samples, drawn as a flame chart:

0         10              30                55   60   70 ms
[task.............................................]
[parseHTML][script...............................]
           [fetch..........][render...........]
                                                   [paint...]

Total time and self time

Sample Total time Direct children Self time
task 60 ms parseHTML 10 + script 45 60 - 55 = 5 ms
parseHTML 10 ms 10 ms
script 45 ms fetch 20 + render 25 0 ms
fetch 20 ms 20 ms
render 25 ms 25 ms

task only does 5 ms by itself, just like the project manager at the start of this lesson. A function with a big total but a small self time (task, script) only passes work to other functions. What needs optimising is the function with the biggest self time, here render. In DevTools, the Bottom-up tab sorts functions by self time, so the optimisation candidate is right on the top row. The Call tree tab shows the call tree from the top function down, sorted by total time.

Long tasks and jank

For animation and scrolling to be smooth at 60 fps (60 frames, or screen pictures, per second), the browser needs a new frame every 16.7 ms (1,000 divided by 60). While a JavaScript task runs, the main thread is busy. Clicks and key presses have to wait, and no new frame can be painted. A screen that stutters because of this is called jank.

A long task is a task that blocks the main thread for more than 50 ms. In the Performance panel it gets a red triangle in its top right corner, and the part beyond 50 ms is striped red. task in the recording above is 60 ms, so task is a long task, and paint (10 ms) isn't. Total Blocking Time (TBT) adds up those parts beyond 50 ms: a 60 ms task adds 10 ms.

How to fix it:

FPS from frame timestamps

FPS (frames per second) is calculated from the gaps between frames: 1000 / average gap. A frame timestamp is the time (in ms) when each frame starts to be drawn.

Frame timestamps (ms) Average gap FPS
[0, 16, 32, 48, 64] 64 / 4 = 16 ms 1000 / 16 = 62.5, rounded 63
[0, 100, 200] 200 / 2 = 100 ms 10
[0, 20, 40, 60] 60 / 3 = 20 ms 50
[0] no gap 0

The average is simply the last timestamp minus the first, divided by the number of gaps (the number of frames minus 1). With fewer than two frames there's no gap at all, so the result is 0.

Measuring directly from code

You can also measure one part of your code without recording. Here are two ways, for the same renderList function:

console.time("render");
renderList(products);
console.timeEnd("render"); // render: 23.4560546875 ms

const start = performance.now();
renderList(products);
console.log(`${(performance.now() - start).toFixed(1)} ms`);

🎯 Lighthouse or Performance?

Lighthouse (the Lighthouse panel, the Analyze page load button) audits one page load. The result is a score with advice, from the metrics FCP, LCP, TBT, CLS and Speed Index. The Performance panel records whatever you do, including clicks and scrolls after the page has loaded, down to each function. Lighthouse tells you that the page is slow; Performance tells you where. Record the production version (the version of the code that visitors use), because a development build is much slower and makes the numbers misleading.

⚠️ Why doesn't the optimisation make a difference?

TL;DR: Record the slow action in the DevTools Performance panel (Ctrl+E), preferably with CPU throttling (the CPU slowed down on purpose to be more like a phone). In the flame chart (a chart of stacked bars in the recording), to the right is time and downwards is calling. Total time is the bar's width. Self time is the total time minus the total time of the functions it calls directly, and what you optimise is the function with the biggest self time (the Bottom-up tab). A task (one piece of browser work without a break) over 50 ms is a long task that makes the page stutter. Fix it by doing less work, splitting it into several tasks, or moving it to a Web Worker (a script that runs outside the main thread, the page's main line of work). In this lesson's example, the task named task has a total of 60 ms, self 5 ms, and is the only long task. FPS (frames, or screen pictures, per second) = 1000 divided by the average gap between frames: 16 ms gaps give 63 fps, 100 ms gaps give 10 fps, and fewer than two frames give 0.

⚡ Record first, don't guess

Meaning: Open the Performance panel in DevTools, click Record (Ctrl+E), do the action that feels slow, then Stop. Turn on CPU throttling (the CPU slowed down on purpose) first, because a developer's laptop is far faster than most users' phones.

⚡ Reading a flame chart

Write:

0         10              30                55   60   70 ms
[task.............................................]
[parseHTML][script...............................]
           [fetch..........][render...........]
                                                   [paint...]

Meaning: This is a flame chart, a picture of a Performance recording. Each bar is one task (one piece of browser work without a break) or function. To the right is time in ms, so the wider a bar, the longer it ran. Downwards is calling: fetch and render are called by script.

⚡ Total time and self time

Guess first: the total time (bar width) of task is 60 ms. Below it are parseHTML 10 ms and script 45 ms. What's its self time, the time task spends on its own work?

5 ms: 60 minus the total time of its direct children (the functions it calls directly), parseHTML 10 and script 45. Those two children do the rest.

Meaning: What needs optimising is the function with the biggest self time, here render (25 ms). The Bottom-up tab sorts functions by self time.

⚡ A long task: over 50 ms

Guess first: a long task is a task that runs for more than 50 ms. task is 60 ms and paint 10 ms. Which is a long task?

Only task. A long task blocks the main thread (the page's main line of work, where JavaScript runs) for more than 50 ms, so clicks and key presses have to wait, and the page stutters.

Meaning: Fix a long task by doing less work, splitting it into several tasks, or moving it to a Web Worker (a script that runs outside the main thread).

What you will learn