Skip to the code

CLS with session windows

Write cls(entries). entries are the layout-shift entries a PerformanceObserver gave the page, in the order they happened: objects with a startTime in milliseconds, a value (the shift's score), and hadRecentInput. Return the page's Cumulative Layout Shift as { value, entries }: the score, and the shifts that make it up.

CLS isn't the sum of every shift. Shifts are grouped into session windows, and CLS is the score of the worst window. Skip a shift whose hadRecentInput is true: it came right after a click, a tap, or a key press, so the reader expected the page to move. It doesn't count, and it doesn't keep a window open.

A shift joins the current window when it starts less than 1 second after that window's last shift and less than 5 seconds after its first. Otherwise it starts a new window. A window's score is the sum of its shifts' values. Return the window with the highest score, the earlier one when two tie, and { value: 0, entries: [] } when there are no shifts that count.

cls([
  { startTime: 1200, value: 0.05, hadRecentInput: false }, // the tea photos load in
  { startTime: 1700, value: 0.08, hadRecentInput: false }, // 500 ms later: the same window, 0.13
  { startTime: 2300, value: 0.3, hadRecentInput: true }, // right after a tap on "Add to basket": skipped
  { startTime: 6000, value: 0.1, hadRecentInput: false }, // a new window, 0.1
]);
// { value: 0.13, entries: [the first two shifts] }

What the tests check

  1. no shifts is a CLS of 0, with no entries
    expect(cls([])).toEqual({ value: 0, entries: [] });
    
  2. shifts less than a second apart add up in one window
    const shift = (startTime, value, hadRecentInput = false) => ({ entryType: "layout-shift", startTime, value, hadRecentInput });
    const shifts = [shift(1000, 0.25), shift(1500, 0.125), shift(2400, 0.0625)];
    const result = cls(shifts);
    expect(result).toEqual({ value: 0.4375, entries: shifts });
    expect(result.entries[0]).toBe(shifts[0]);
    
  3. a gap of a second or more starts a new window, and CLS is the largest one
    const shift = (startTime, value, hadRecentInput = false) => ({ entryType: "layout-shift", startTime, value, hadRecentInput });
    const first = [shift(0, 0.125), shift(500, 0.125)];
    const second = [shift(1500, 0.25), shift(2400, 0.25)];
    expect(cls([...first, ...second])).toEqual({ value: 0.5, entries: second });
    
  4. a window closes 5 seconds after its first shift, even while shifts keep coming
    const shift = (startTime, value, hadRecentInput = false) => ({ entryType: "layout-shift", startTime, value, hadRecentInput });
    const steady = [0, 900, 1800, 2700, 3600, 4500, 5400].map((time) => shift(time, 0.0625));
    expect(cls(steady)).toEqual({ value: 0.375, entries: steady.slice(0, 6) });
    const edge = [0, 900, 1800, 2700, 3600, 4500, 5000].map((time) => shift(time, 0.125));
    expect(cls(edge)).toEqual({ value: 0.75, entries: edge.slice(0, 6) });
    
  5. shifts right after input don't count, and don't keep a window open
    const shift = (startTime, value, hadRecentInput = false) => ({ entryType: "layout-shift", startTime, value, hadRecentInput });
    const shifts = [shift(0, 0.25), shift(800, 0.5, true), shift(1600, 0.125)];
    expect(cls(shifts)).toEqual({ value: 0.25, entries: [shifts[0]] });
    expect(cls([shift(300, 0.5, true)])).toEqual({ value: 0, entries: [] });
    
  6. when two windows tie, the earlier one is the CLS
    const shift = (startTime, value, hadRecentInput = false) => ({ entryType: "layout-shift", startTime, value, hadRecentInput });
    const shifts = [shift(0, 0.25), shift(2000, 0.25)];
    expect(cls(shifts)).toEqual({ value: 0.25, entries: [shifts[0]] });
    
  7. a late, sharp burst beats an early, long trickle
    const shift = (startTime, value, hadRecentInput = false) => ({ entryType: "layout-shift", startTime, value, hadRecentInput });
    const trickle = Array.from({ length: 10 }, (_, index) => shift(index * 500, 0.03125));
    const burst = [shift(10000, 0.25), shift(10200, 0.25)];
    expect(cls([...trickle, ...burst])).toEqual({ value: 0.5, entries: burst });
    expect(cls(trickle).value).toBe(0.3125);
    

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