Skip to the code

Frame rate report

The tea shop's menu animates a basket badge, and you want to know how smooth it is on real phones. A requestAnimationFrame loop that records each callback's timestamp gets one number for every frame the page rendered. Write frameReport(timestamps, options), which turns those timestamps (milliseconds, in order) into { frames, fps, dropped, longest }.

A frame lasts 1000 / refreshRate ms, where options.refreshRate is 60 unless given, so about 16.7 ms. First drop any timestamp equal to the one before it: callbacks that run in the same frame all get that frame's time. Then take the gaps between consecutive timestamps. frames is the number of gaps. fps is frames divided by the seconds from the first timestamp to the last. A gap longer than one frame plus options.tolerance (2 ms unless given) means the screen showed the same picture for more than one refresh: it adds Math.round(gap / frame) - 1 to dropped, and at least 1. longest is the longest gap. Round fps and longest to one decimal place, at the end.

With fewer than two different timestamps there are no gaps, and the report is { frames: 0, fps: 0, dropped: 0, longest: 0 }.

frameReport([0, 16.7, 33.4, 83.4, 100.1, 116.8]);
// The 50 ms gap held three frames' time, so two frames were dropped:
// { frames: 5, fps: 42.8, dropped: 2, longest: 50 }

What the tests check

  1. a smooth second at 60 Hz
    const timestamps = Array.from({ length: 61 }, (_, index) => (index * 1000) / 60);
    expect(frameReport(timestamps)).toEqual({ frames: 60, fps: 60, dropped: 0, longest: 16.7 });
    
  2. a long gap counts the frames it held
    expect(frameReport([0, 16.7, 33.4, 83.4, 100.1, 116.8])).toEqual({ frames: 5, fps: 42.8, dropped: 2, longest: 50 });
    
  3. a gap is dropped frames only past one frame plus the tolerance, and then at least one
    expect(frameReport([0, 18.6, 37.2]).dropped).toBe(0);
    expect(frameReport([0, 18.7]).dropped).toBe(1);
    expect(frameReport([0, 16.7, 116.7])).toEqual({ frames: 2, fps: 17.1, dropped: 5, longest: 100 });
    
  4. takes another tolerance
    expect(frameReport([0, 17], { tolerance: 0 }).dropped).toBe(1);
    expect(frameReport([0, 21], { tolerance: 5 }).dropped).toBe(0);
    
  5. takes another refresh rate
    const smooth = Array.from({ length: 121 }, (_, index) => (index * 1000) / 120);
    expect(frameReport(smooth, { refreshRate: 120 })).toEqual({ frames: 120, fps: 120, dropped: 0, longest: 8.3 });
    expect(frameReport([0, 8.3, 16.7, 33.3], { refreshRate: 120 })).toEqual({ frames: 3, fps: 90.1, dropped: 1, longest: 16.6 });
    
  6. a repeated timestamp is the same frame
    expect(frameReport([0, 0, 16.7, 16.7, 33.4])).toEqual({ frames: 2, fps: 59.9, dropped: 0, longest: 16.7 });
    
  7. fewer than two different timestamps is an empty report
    const empty = { frames: 0, fps: 0, dropped: 0, longest: 0 };
    expect(frameReport([])).toEqual(empty);
    expect(frameReport([12])).toEqual(empty);
    expect(frameReport([5, 5])).toEqual(empty);
    
  8. reads the timestamps of a real requestAnimationFrame loop with a long task in it
    const clock = useFakeTimers();
    const timestamps = [];
    function record(time) {
      timestamps.push(time);
      if (timestamps.length === 3) clock.spend(40);
      if (timestamps.length < 10) requestAnimationFrame(record);
    }
    requestAnimationFrame(record);
    await clock.runAllAsync();
    // The fake clock renders a frame every 16 ms; the 40 ms of work pushes one frame back.
    expect(timestamps).toEqual([16, 32, 48, 96, 112, 128, 144, 160, 176, 192]);
    expect(frameReport(timestamps)).toEqual({ frames: 9, fps: 51.1, dropped: 2, longest: 48 });
    

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).