Speed Index measures how quickly a page's visible content fills in, not just when it's done. A lab tool films the page as it loads and scores each frame's visual progress, from 0 (nothing of the final page on screen) to 1 (it looks like the last frame). Write speedIndex(frames). frames are { time, progress }, with time in ms. Return the Speed Index in ms.
WebPageTest, where Speed Index comes from, defines it as the area above the visual progress curve: for every interval between two frames, the interval's length times the share of the page still missing, interval * (1 - progress), all added up. Sort a copy of the frames by time first. The curve is a staircase, because the screen doesn't change between frames: the interval from one frame to the next uses the earlier frame's progress. The area starts at the first frame, so nothing before its time counts (a filmstrip's first frame is normally at 0, the start of the navigation), and it ends at the last frame, whose own progress never multiplies anything.
Progress can go down as well as up (a banner that slides away), and the formula takes it as it is. WebPageTest truncates the result to whole milliseconds, so return Math.floor(total). Fewer than two frames is 0.
speedIndex([
{ time: 0, progress: 0 },
{ time: 800, progress: 0.5 }, // the header and the tea menu's text
{ time: 1200, progress: 0.75 }, // the first photos of the teas
{ time: 2000, progress: 1 }, // visually complete
]);
// 800 * (1 - 0) + 400 * (1 - 0.5) + 800 * (1 - 0.75) = 1200
const frames = [{ time: 0, progress: 0 }, { time: 800, progress: 0.5 }, { time: 1200, progress: 0.75 }, { time: 2000, progress: 1 }];
expect(speedIndex(frames)).toBe(1200);
expect(speedIndex([{ time: 0, progress: 0 }, { time: 1500, progress: 1 }])).toBe(1500);
expect(speedIndex([{ time: 0, progress: 0 }, { time: 1500, progress: 1 }, { time: 2500, progress: 1 }, { time: 4000, progress: 1 }])).toBe(1500);
expect(speedIndex([{ time: 0, progress: 0 }, { time: 1000, progress: 0.5 }, { time: 2000, progress: 0.5 }])).toBe(1500);
expect(speedIndex([{ time: 0, progress: 0 }, { time: 1000, progress: 0.5 }, { time: 2000, progress: 1 }])).toBe(1500);
const frames = [{ time: 1200, progress: 0.75 }, { time: 0, progress: 0 }, { time: 2000, progress: 1 }, { time: 800, progress: 0.5 }];
expect(speedIndex(frames)).toBe(1200);
expect(frames.map((frame) => frame.time)).toEqual([1200, 0, 2000, 800]);
expect(speedIndex([{ time: 300, progress: 0 }, { time: 1300, progress: 1 }])).toBe(1000);
expect(speedIndex([{ time: 200, progress: 0.5 }, { time: 600, progress: 1 }])).toBe(200);
const frames = [{ time: 0, progress: 0 }, { time: 500, progress: 1 }, { time: 1000, progress: 0.5 }, { time: 1500, progress: 1 }];
expect(speedIndex(frames)).toBe(750);
expect(speedIndex([{ time: 0, progress: 0 }, { time: 33, progress: 0.3 }, { time: 100, progress: 1 }])).toBe(79);
expect(speedIndex([{ time: 0, progress: 0 }, { time: 250, progress: 0.875 }, { time: 1000, progress: 1 }])).toBe(343);
expect(speedIndex([])).toBe(0);
expect(speedIndex([{ time: 500, progress: 1 }])).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 Speed Index from a filmstrip. 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.