Sub topic 6
Spotting long tasks that block the main thread
This chapter assumes you can record a trace in the Performance panel and know the basic layout of the flame chart. It also assumes you know that JavaScript runs one task at a time on the main thread.
What a long task is
A task is one unit of work the browser runs on the main thread, such as an event handler, a timer callback, or a rendering step. A long task is any task that runs for more than 50 ms. While it runs, the browser cannot respond to clicks, typing, or scrolling. Users experience this as lag, and it raises metrics such as Total Blocking Time and Interaction to Next Paint.
Finding long tasks in a trace
- Record a trace while you repeat the slow interaction, such as typing in a filter box.
- Open the Main track and look at the top-level bars. Each one is a task.
- Look for tasks marked with a red triangle in the corner and a red hatched area. DevTools applies this marking to the part of a task beyond 50 ms.
- Click the task. The summary at the bottom shows its duration. Then read the flame chart under it, from top to bottom, to see which functions account for the time.
The widest bar at the bottom of a stack is usually where the time is spent. A tall stack is not a problem in itself. A wide one is.
Linking a long task to Angular
In most Angular apps, a long task begins with a browser event, such as a click or an input. Zone.js notices that the callback finished and tells Angular. Angular then runs change detection inside the same task, so the stack usually has this shape:
Compare the width of your handler's frames with the width of the change detection frames. If the handler dominates, the slow code is yours. If change detection dominates, look at template expressions and how many components get checked. Later chapters cover both cases.
Detecting long tasks from code
You can also log long tasks while you use the app. The Long Tasks API reports them as they happen:
const observer = new PerformanceObserver(list => {
for (const entry of list.getEntries()) {
console.log(
`Long task: ${Math.round(entry.duration)} ms at ${Math.round(entry.startTime)} ms`
);
}
});
observer.observe({ type: 'longtask', buffered: true });Paste this into the Console. The log tells you that a long task happened and when. It does not tell you which code caused it, so use the trace for that.
Common mistakes
- Profiling the development build. Extra checks make tasks longer than users will see. Use a production-like build.
- Skipping CPU throttling. On a fast laptop, a 40 ms task may become 160 ms on a mid-range phone.
- Looking at the average. One 300 ms task hurts more than many 5 ms tasks, so find the longest ones first.
- Splitting blame wrongly. A long task may contain several unrelated pieces of work. Read the flame chart before you decide what to fix.
Exercise
In a trace, you find a 180 ms task with a red triangle. Its flame chart is mostly wide change detection frames, and your own click handler takes 5 ms. Is the handler the main cause, and where should you look next?
Show answer
No. The handler accounts for only 5 ms of the 180 ms. Most of the time goes to change detection, so look next at what Angular checks during that pass: expensive template expressions, components that could use OnPush, and how many components are rechecked.