Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท Performance Panel

Long Tasks and Main Thread Work

Why the browser's single main thread matters, how DevTools marks tasks over 50 ms, how Total Blocking Time is computed, and ways to break up long tasks.

JavaScript, style calculation, layout and most painting preparation all share one thread, the main thread. While it is busy with one job, it cannot respond to a click, a key press or a scroll. A task that holds the thread for a long time is called a long task, and it is the most common cause of a page that feels sluggish.

This lesson shows how to find long tasks in DevTools, how they turn into measurable numbers like Total Blocking Time, and what you can do to break them up.

What counts as a long task

The browser runs work in tasks: an event handler, a timer callback, parsing a chunk of HTML, running a script. By convention a task longer than 50 ms is long. Why 50? If the user taps while a task is running, the page cannot respond until the task finishes. Research on perception suggests responses within about 100 ms feel instant, and the rest of that budget is needed for the response itself, so a 50 ms ceiling per task keeps input delay small.

In the Performance panel, long tasks appear in the Main track with a red triangle and red diagonal stripes on the part beyond 50 ms. The Bottom-Up tab with "Group by Activity" or "Group by Domain" shows who is responsible.

Total Blocking Time

Lighthouse and many lab tools summarize long tasks with Total Blocking Time (TBT): for every task between First Contentful Paint and Time to Interactive, take the part over 50 ms and add them up. A 120 ms task contributes 70 ms; a 40 ms task contributes nothing.

JS Stand-in: compute Total Blocking Time

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The 180 ms boot task alone produces 130 ms of blocking time. Splitting it into four 45 ms tasks would bring its contribution to zero, even though the total work is the same, which shows why TBT measures responsiveness, not total work.

Finding the cause

Click a long task and look at what is on top of it in the flame chart:

  • A wide yellow block of your own functions: algorithmic cost; look at Bottom-Up for the top self-time function.
  • A third-party script (tag manager, ads, chat widget): group by "Third Party".
  • Purple Recalculate Style or Layout blocks: expensive CSS selectors, or layout triggered repeatedly.
  • "Evaluate Script" or "Compile Code": a big bundle. Split it, defer it, or remove unused code with the Coverage tool.
  • Garbage collection (grey "Major GC" or "Minor GC"): too many short-lived allocations.

DevTools also flags forced reflows: JavaScript that reads a layout value (for example offsetHeight) right after changing the DOM forces the browser to compute layout synchronously, in the middle of your script. The warning is a red corner on the layout event with the text "Forced reflow" and a link to the offending line.

Breaking up long tasks

The goal is to give the browser a chance to handle input between chunks of work. Options, from simplest to most advanced:

  1. Do less: remove unused code, debounce handlers, avoid work for off-screen content.
  2. Yield to the main thread inside long loops with await scheduler.yield() (where supported) or await new Promise(r => setTimeout(r)).
  3. Chunk the work by processing a slice of the array per task.
  4. Move heavy computation to a Web Worker, which runs on another thread and cannot block input.
  5. Defer non-critical scripts with defer, async or dynamic import() after the first render.

Here is a stand-in that simulates processing 10,000 items either in one task or in chunks, and reports the longest block of "main thread" time using a fixed cost per item so the numbers are deterministic.

JS Stand-in: one big task versus chunks

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The total work is 200 ms in every row. As one task it blocks for 150 ms; as 2,000-item chunks or smaller, no task exceeds 50 ms and the blocking time is zero. Between the chunks the browser can process clicks. The cost is a slightly longer total wall time, which is usually the right trade.

A real version of the chunking pattern looks like this (read-only because the Performance panel is not available here):

JavaScript
async function processAll(items, handle) {
  let last = performance.now();
  for (const item of items) {
    handle(item);
    if (performance.now() - last > 40) {
      await new Promise(resolve => setTimeout(resolve));   // let the browser breathe
      last = performance.now();
    }
  }
}

Verify with a second recording

After you change the code, record the same interaction again. In the Main track the single red block should now be a series of short blocks separated by gaps, and in the Interactions track the INP-style numbers should shrink. Always compare numbers, not impressions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How long must a task run to be called a long task?

    The long task threshold is 50 ms.
  2. 2. A 120 ms task and a 40 ms task occur. What do they contribute to Total Blocking Time?

    Only the part above 50 ms counts: 120 - 50 = 70, and the 40 ms task contributes 0.
  3. 3. Which technique lets the browser handle clicks during heavy work?

    Splitting work into chunks and yielding gives the event loop a chance to run input handlers.
  4. 4. What does a forced reflow warning mean?

    Reading properties like offsetHeight after a DOM write makes the browser compute layout immediately, which can be expensive in loops.

Summary

  • The main thread runs JavaScript, style, layout and more, so one long task delays input.
  • Tasks over 50 ms are long; Total Blocking Time sums the excess.
  • Find causes with the flame chart and Bottom-Up grouping; watch for forced reflows and third-party scripts.
  • Fix by doing less, yielding, chunking, using workers and deferring scripts.
  • Re-record to prove the improvement.

Next: The Application Panel at a Glance