Break up the long tasks that keep your page from responding

Avoid long main-thread tasks is the Lighthouse and PageSpeed Insights diagnostic that lists the longest tasks the browser ran on its main thread while the page loaded, counting any task of 50 milliseconds or more. While a task runs, the browser can't respond to a click or a key press, so long tasks during load make a page that looks ready feel stuck. The diagnostic kept its name in Lighthouse 13 and doesn't affect the score by itself, but Total Blocking Time, which is built from the same long tasks, does.

What the long tasks table shows

  • URL

    The script Lighthouse attributes the task to, which tells you whose code to look at first.

  • Start Time

    When the task began during the load.

  • Duration

    How long the task ran. In a simulated mobile run this is Lighthouse's estimate for a slowed processor, not the time your own machine took.

  • How many it lists

    Up to 20 tasks, longest first, and the summary line says how many it shows. A page with no task of 50 ms or more gets no table.

The 50 ms cutoff, the 20-task limit and the columns come from the audit's source code, and web.dev's guide to long tasks explains why tasks over 50 ms delay input.

How long tasks add up to Total Blocking Time

  • A task's blocking time is whatever it runs past 50 milliseconds. Total Blocking Time adds up the blocking time of every long task after First Contentful Paint, so in web.dev's example a 250 ms task adds 200 ms and a 90 ms task adds 40. A 45 ms task adds nothing.

  • That's why breaking a 300 ms task into pieces under 50 ms each can take its blocking time to zero without removing any work: between the pieces, the browser gets a chance to handle input and paint. Total Blocking Time carries 30% of the Lighthouse performance score in Chrome's scoring guide, more than any other metric.

Yield to the main thread between chunks of work

// Let the browser handle input and paint before continuing
function yieldToMain() {
  if (globalThis.scheduler?.yield) return scheduler.yield();
  return new Promise((resolve) => setTimeout(resolve, 0));
}

async function processItems(items) {
  let lastYield = performance.now();
  for (const item of items) {
    processItem(item);
    // Yield about every 50 ms rather than after every item
    if (performance.now() - lastYield > 50) {
      await yieldToMain();
      lastYield = performance.now();
    }
  }
}

How to fix long main-thread tasks

  1. Match each task to its codeStart with the longest task and the URL beside it. Record the load in the DevTools Performance panel, where long tasks carry a red corner, select one and open Bottom-Up to see which functions took the time.
  2. Run the visible part firstDo the work the visitor sees first, such as updating the screen, then yield and run saving, logging and analytics in a later task.
  3. Process big data in chunksLoop over large arrays in slices and yield between them, as in the code above. Not every browser supports scheduler.yield() yet, so keep the setTimeout fallback.
  4. Deal with third-party tasksIf the longest tasks belong to a tag or widget, you can't split them yourself. Remove the tag, load it once the page has finished loading or replace it with a lighter one.

Frequently asked questions

Does fixing long tasks help INP?

Often. A click that lands during a long task waits for the task to finish before its handler can start, and that wait counts toward Interaction to Next Paint as input delay. web.dev's long tasks guide shows how breaking tasks up lets the browser respond sooner.

Is setTimeout enough to break up a task?

It works, but code deferred with setTimeout runs after any other tasks already waiting, and after five levels of nested timeouts the browser adds a minimum delay to each one. scheduler.yield() resumes your code ahead of other tasks of the same priority, while still letting input go first, so your work keeps its order. web.dev's long tasks guide compares the two.

Why does this audit list tasks under 50 ms on my computer?

Mobile runs estimate how long each task would take on a processor four times slower, following Lighthouse's throttling settings. A 20 ms task on a fast laptop can come out at 80 ms. Turn on CPU throttling in the Performance panel to see the same tasks yourself.