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
const tasks = [{ start: 1000, duration: 120 }, { start: 1500, duration: 70 }, { start: 2000, duration: 30 }];
expect(totalBlockingTime(tasks, 800, 3000)).toBe(90);
expect(totalBlockingTime([{ start: 1000, duration: 50 }], 800, 3000)).toBe(0);
expect(totalBlockingTime([{ start: 1000, duration: 51 }], 800, 3000)).toBe(1);
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);
expect(totalBlockingTime([{ start: 0, duration: 150 }], 50, 1000)).toBe(50);
expect(totalBlockingTime([{ start: 300, duration: 140 }], 400, 3000)).toBe(0);
expect(totalBlockingTime([{ start: 2900, duration: 200 }], 400, 3000)).toBe(50);
expect(totalBlockingTime([{ start: 2980, duration: 300 }], 400, 3000)).toBe(0);
expect(totalBlockingTime([{ start: 0, duration: 5000 }], 1000, 1200)).toBe(150);
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);
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).
I'm here to help with Total Blocking Time. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.