Skip to the code

Speed Index from a filmstrip

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

What the tests check

  1. adds up each interval times what was still missing at its start
    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);
    
  2. a page that paints everything at once scores the moment it painted
    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);
    
  3. the last frame's progress never counts
    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);
    
  4. sorts a copy of the frames by time
    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]);
    
  5. the area starts at the first frame
    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);
    
  6. progress that goes back down counts as it is
    const frames = [{ time: 0, progress: 0 }, { time: 500, progress: 1 }, { time: 1000, progress: 0.5 }, { time: 1500, progress: 1 }];
    expect(speedIndex(frames)).toBe(750);
    
  7. truncates to whole milliseconds
    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);
    
  8. fewer than two frames is 0
    expect(speedIndex([])).toBe(0);
    expect(speedIndex([{ time: 500, progress: 1 }])).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).