Skip to the code

Total Blocking Time

Total Blocking Time is Lighthouse's lab measure of how long the main thread was too busy to answer a tap while the page loaded. Write totalBlockingTime(tasks, fcp, tti). tasks are the main thread's top-level tasks, { start, duration } in ms, and fcp and tti are the times of First Contentful Paint and Time to Interactive. Return TBT as Lighthouse 13.6.0 computes it.

Only the time between fcp and tti counts. Clip each task to that range first: it runs from the later of its start and fcp to the earlier of its end (start + duration) and tti. The first 50 ms of a clipped task are fine, and anything past them is blocking time, so a clipped task of 50 ms or less adds nothing and one of 120 ms adds 70. A task that ends before fcp or starts after tti adds nothing, and TBT is 0 when tti isn't after fcp.

totalBlockingTime([
  { start: 300, duration: 200 }, // across FCP: clipped to 400 to 500, 100 ms, so 50 blocking
  { start: 900, duration: 120 }, // rendering the tea menu: 70 blocking
  { start: 1500, duration: 45 }, // short: nothing
  { start: 2950, duration: 150 }, // across TTI: clipped to 2950 to 3000, 50 ms, so nothing
], 400, 3000);
// 120

What the tests check

  1. adds up the part of each task past 50 ms
    const tasks = [{ start: 1000, duration: 120 }, { start: 1500, duration: 70 }, { start: 2000, duration: 30 }];
    expect(totalBlockingTime(tasks, 800, 3000)).toBe(90);
    
  2. a 50 ms task blocks nothing, and a 51 ms task blocks 1
    expect(totalBlockingTime([{ start: 1000, duration: 50 }], 800, 3000)).toBe(0);
    expect(totalBlockingTime([{ start: 1000, duration: 51 }], 800, 3000)).toBe(1);
    
  3. tasks that end before FCP or start after TTI don't count
    expect(totalBlockingTime([{ start: 0, duration: 300 }], 400, 3000)).toBe(0);
    expect(totalBlockingTime([{ start: 100, duration: 300 }], 400, 3000)).toBe(0);
    expect(totalBlockingTime([{ start: 3500, duration: 400 }], 400, 3000)).toBe(0);
    
  4. a task across FCP counts only from FCP, and then loses its first 50 ms
    expect(totalBlockingTime([{ start: 0, duration: 150 }], 50, 1000)).toBe(50);
    expect(totalBlockingTime([{ start: 300, duration: 140 }], 400, 3000)).toBe(0);
    
  5. a task across TTI counts only up to TTI
    expect(totalBlockingTime([{ start: 2900, duration: 200 }], 400, 3000)).toBe(50);
    expect(totalBlockingTime([{ start: 2980, duration: 300 }], 400, 3000)).toBe(0);
    
  6. a task that covers both ends is clipped at both
    expect(totalBlockingTime([{ start: 0, duration: 5000 }], 1000, 1200)).toBe(150);
    
  7. no time between FCP and TTI, or no tasks, is a TBT of 0
    const tasks = [{ start: 1000, duration: 400 }];
    expect(totalBlockingTime(tasks, 1200, 1200)).toBe(0);
    expect(totalBlockingTime(tasks, 1300, 1100)).toBe(0);
    expect(totalBlockingTime([], 400, 3000)).toBe(0);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).