Skip to the code

Layout shift score

When an element on the tea shop's page moves between two frames without the reader doing anything, the browser reports a layout-shift entry, and its value is the shift's score. Write layoutShiftScore(before, after, viewport), the score of a frame in which one element moved, as the Layout Instability spec defines it. before and after are where the element's box was in the previous frame and is now, as { x, y, width, height } in the viewport's coordinates, the way getBoundingClientRect() gives them (so they can be negative or past the viewport's edges), and viewport is { width, height }. Return { value, impactFraction, distanceFraction }.

The score is the impact fraction times the distance fraction. The impact fraction is how much of the viewport the shift touched: the area of the union of the element's visible part before and its visible part after, divided by the viewport's area. An element's visible part is its box clipped to the viewport, from 0 to viewport.width across and 0 to viewport.height down; what's outside doesn't count. The distance fraction is how far it moved: the larger of the horizontal and the vertical distance its starting point moved, divided by the viewport's larger side (its width or its height), and never more than 1. On a page written left to right and top to bottom, an element's starting point is its box's top-left corner, (x, y), before any clipping.

Not every change is a shift. The spec lets each browser choose how many pixels a starting point has to move to count, and Chrome's number is 3: an element that moved less than 3 px across and less than 3 px down hasn't shifted, so all three numbers are 0. A box that only grows or shrinks keeps its starting point, so it hasn't shifted either. Chrome also ignores an element that was outside the viewport in both frames, since nobody saw it move: all three numbers are 0 then too. The spec has one more exception, for elements that slide into or out of view sideways, which the next problem, layout-shift-many-elements, adds.

// web.dev's example: an element that fills the top half of the viewport drops by a quarter of its height.
layoutShiftScore({ x: 0, y: 0, width: 400, height: 400 }, { x: 0, y: 200, width: 400, height: 400 }, { width: 400, height: 800 });
// { value: 0.1875, impactFraction: 0.75, distanceFraction: 0.25 }
// Before and after together cover y = 0 to 600, 0.75 of the viewport; it moved 200 of the 800 px.

What the tests check

  1. web.dev's example: half the viewport drops a quarter of the way
    const viewport = { width: 400, height: 800 };
    expect(layoutShiftScore({ x: 0, y: 0, width: 400, height: 400 }, { x: 0, y: 200, width: 400, height: 400 }, viewport)).toEqual({ value: 0.1875, impactFraction: 0.75, distanceFraction: 0.25 });
    
  2. only the part inside the viewport counts toward the impact
    const viewport = { width: 400, height: 800 };
    // The bottom half drops by 200 px: its lower half goes out of view.
    expect(layoutShiftScore({ x: 0, y: 400, width: 400, height: 400 }, { x: 0, y: 600, width: 400, height: 400 }, viewport)).toEqual({ value: 0.125, impactFraction: 0.5, distanceFraction: 0.25 });
    
  3. the distance is the larger of the horizontal and vertical moves, and overlap counts once
    const close = (result, expected) => {
      expect(Object.keys(result).sort()).toEqual(Object.keys(expected).sort());
      for (const key of Object.keys(expected)) expect(result[key]).toBeCloseTo(expected[key], 9);
    };
    const viewport = { width: 400, height: 800 };
    // 80 px left and 40 px down; the two boxes overlap by 120 by 120.
    close(layoutShiftScore({ x: 100, y: 100, width: 200, height: 160 }, { x: 20, y: 140, width: 200, height: 160 }, viewport), { value: 0.0155, impactFraction: 0.155, distanceFraction: 0.1 });
    
  4. the distance is divided by the viewport's larger side
    const close = (result, expected) => {
      expect(Object.keys(result).sort()).toEqual(Object.keys(expected).sort());
      for (const key of Object.keys(expected)) expect(result[key]).toBeCloseTo(expected[key], 9);
    };
    const landscape = { width: 1000, height: 500 };
    close(layoutShiftScore({ x: 0, y: 0, width: 500, height: 250 }, { x: 0, y: 100, width: 500, height: 250 }, landscape), { value: 0.035, impactFraction: 0.35, distanceFraction: 0.1 });
    
  5. a move of less than 3 px each way isn't a shift; 3 px is
    const close = (result, expected) => {
      expect(Object.keys(result).sort()).toEqual(Object.keys(expected).sort());
      for (const key of Object.keys(expected)) expect(result[key]).toBeCloseTo(expected[key], 9);
    };
    const viewport = { width: 400, height: 800 };
    const box = { x: 10, y: 10, width: 100, height: 100 };
    expect(layoutShiftScore(box, { ...box, x: 12, y: 12.5 }, viewport)).toEqual({ value: 0, impactFraction: 0, distanceFraction: 0 });
    close(layoutShiftScore(box, { ...box, x: 13 }, viewport), { value: 0.0321875 * 0.00375, impactFraction: 0.0321875, distanceFraction: 0.00375 });
    
  6. an element coming into view counts only where it's visible
    const viewport = { width: 400, height: 800 };
    expect(layoutShiftScore({ x: 0, y: 900, width: 400, height: 200 }, { x: 0, y: 700, width: 400, height: 200 }, viewport)).toEqual({ value: 0.03125, impactFraction: 0.125, distanceFraction: 0.25 });
    
  7. an element out of view before and after doesn't count
    const viewport = { width: 400, height: 800 };
    const none = { value: 0, impactFraction: 0, distanceFraction: 0 };
    expect(layoutShiftScore({ x: 0, y: 900, width: 400, height: 100 }, { x: 0, y: 1000, width: 400, height: 100 }, viewport)).toEqual(none);
    expect(layoutShiftScore({ x: 0, y: -300, width: 400, height: 200 }, { x: 0, y: -250, width: 400, height: 200 }, viewport)).toEqual(none);
    
  8. the distance fraction is never more than 1
    const viewport = { width: 400, height: 800 };
    expect(layoutShiftScore({ x: 0, y: -1200, width: 400, height: 1400 }, { x: 0, y: 300, width: 400, height: 1400 }, viewport)).toEqual({ value: 0.875, impactFraction: 0.875, distanceFraction: 1 });
    
  9. a box that only grows hasn't shifted
    const viewport = { width: 400, height: 800 };
    expect(layoutShiftScore({ x: 0, y: 100, width: 400, height: 200 }, { x: 0, y: 100, width: 400, height: 500 }, viewport)).toEqual({ value: 0, impactFraction: 0, distanceFraction: 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).