Sub topic 9
Recording and reading a performance profile
This chapter assumes you can use the Sources panel and the Network panel, and that you know how a browser turns HTML, CSS and JavaScript into pixels. You will record a profile in the Performance panel, read its tracks, and find the long tasks that make a page feel slow.
What the Performance panel records
The Performance panel records everything the browser's main thread does over a period of time. The main thread is the single thread that runs your JavaScript, calculates styles, lays out the page and paints it. If one job on that thread runs for a long time, clicks, scrolling and animations all wait.
A recording shows these kinds of work:
| Activity | Meaning |
|---|---|
| Scripting | Running JavaScript |
| Rendering | Style calculation and layout |
| Painting | Drawing pixels into layers |
| System and Idle | Browser overhead and free time |
A long task is any task on the main thread that runs for more than 50 ms. DevTools marks long tasks with a red triangle and a red striped bar. Long tasks are the first thing to look for.
Recording a profile
Record in a way that reflects what real users see.
- Open DevTools and select the Performance panel.
- Open an Incognito window first, so extensions do not add work to the trace.
- Optionally set CPU throttling to 4x slowdown. Your development machine is usually faster than a user's phone.
- Click Record, or press Ctrl+E (Cmd+E on macOS).
- Do the action you want to measure, such as a click or a scroll.
- Click Stop. Keep recordings short, because long ones are hard to read.
To measure page load, use the Start profiling and reload page button instead. It reloads the page and stops recording after load.
Reading the recording
The top of the panel shows an overview with FPS, CPU and network graphs. Drag across it to choose a time range. The main area below is split into tracks.
- Frames shows each frame the browser produced. Long or red frames mean dropped frames.
- Interactions marks user input and how long the page took to respond.
- Timings shows markers such as First Contentful Paint (FCP) and Largest Contentful Paint (LCP). It also shows your own User Timing measures.
- Main shows a flame chart of the main thread.
In a flame chart, each bar is a function call. A bar's width is its duration. A bar stacked below another was called by it. The widest bar at the bottom of a stack is often where the time went.
Adding your own markers
You can label your code so it appears in the Timings track. This example uses User Timing marks and a measure.
performance.mark('render-start');
const list = document.createElement('ul');
for (let i = 0; i < 1000; i++) {
const item = document.createElement('li');
item.textContent = 'Item ' + i;
list.appendChild(item);
}
document.body.appendChild(list);
performance.mark('render-end');
performance.measure('render-list', 'render-start', 'render-end');Record while this code runs. A render-list entry appears in Timings. Its width matches the duration of your code, so you can compare it with the browser's own work underneath.
Finding the cause of a long task
- Find a red-marked task in the Main track and click it.
- Read the Summary tab at the bottom. It splits the time into scripting, rendering and painting.
- Open the Bottom-Up tab to sort functions by self time. Self time is the time spent in a function itself, excluding the functions it called.
- Click a function name to jump to its source in the Sources panel.
The usual causes and fixes are:
| Symptom | Likely cause | Typical fix |
|---|---|---|
| Wide yellow Scripting bar | Heavy JavaScript | Split the work into smaller chunks, or move it to a Web Worker |
| Purple Layout bar repeated many times | Layout thrashing: reading layout values and writing styles in a loop | Batch reads first, then writes |
| Large green Paint bar | Expensive paint | Reduce the area that changes, or avoid costly effects |
Common mistakes
- Recording for too long, so the interesting task is lost in noise.
- Measuring with a fast desktop CPU and no throttling, then missing problems that appear on phones.
- Optimizing a function that is wide but not on the main thread's critical path. Check the task's total duration first.
- Reading the flame chart as a timeline of function order. Width means time. Vertical position means call depth.
Exercise
In the Main track you see a task of 180 ms. Inside it, a function called buildRows has a self time of 150 ms. Is this a long task, and what would you look at next?
Show answer
Yes. It is far over the 50 ms threshold. Open the Bottom-Up tab and click buildRows to jump to its source. Since most time is spent in the function itself, look for loops or heavy work there. Then split the work into smaller chunks or move it off the main thread.
Exercise
Why might you set CPU throttling to 4x slowdown before recording?
Show answer
Developer machines are usually faster than users' devices. Throttling makes the main thread slower, so long tasks that would be hidden on your machine become visible.