Skip to the code

Layout shift with many elements

Most layout shifts move several elements at once: a banner loads at the top of the tea shop's menu, and every card below it drops. Write layoutShift(elements, viewport), the score of a frame in which many elements may have moved. Each element is { id, before, after }, with before and after as in the layout-shift-score problem: { x, y, width, height } in the viewport's coordinates, which may be negative or past its edges. viewport is { width, height }. Return { value, impactFraction, distanceFraction, shifted }, where shifted lists the ids of the elements that count, in the order they were given.

Decide element by element whether it counts, as Chrome does. An element whose starting point (its box's top-left corner, before clipping) moved less than 3 px across and less than 3 px down hasn't shifted. Clip each box to the viewport to get its visible part; an element that was out of view in both frames doesn't count. An element that comes into view or leaves it (its visible part is empty in one of the frames) while moving less than 3 px up or down doesn't count either: the spec calls it an inline clip crosser, and leaves it out so a carousel can slide its pictures in from the side. Every other element counts.

The impact region is every point in the visible part, before or after, of any element that counts, and the impact fraction is its area divided by the viewport's area. A point covered by several elements counts once, so you need the exact area of a union of rectangles: adding up each element's area counts overlaps twice, and taking away the overlap of each pair still goes wrong where three rectangles overlap. The distance fraction is the largest move of any element that counts (for each, the larger of its horizontal and vertical moves), divided by the viewport's larger side, and at most 1. The value is their product. When no element counts, all three are 0 and shifted is empty.

// A 400 by 800 viewport: "assam" stays put, and the three rows below it drop.
const row = (y) => ({ x: 0, y, width: 400, height: 100 });
layoutShift([
  { id: "assam", before: row(0), after: row(0) },
  { id: "darjeeling", before: row(100), after: row(200) },
  { id: "oolong", before: row(200), after: row(400) },
  { id: "sencha", before: row(300), after: row(600) },
], { width: 400, height: 800 });
// { value: 0.234375, impactFraction: 0.625, distanceFraction: 0.375, shifted: ["darjeeling", "oolong", "sencha"] }
// The region covers y = 100 to 500 and 600 to 700; sencha moved furthest, 300 of 800 px.

What the tests check

  1. rows that drop below a row that stays put
    const row = (y) => ({ x: 0, y, width: 400, height: 100 });
    const result = layoutShift([
      { id: "assam", before: row(0), after: row(0) },
      { id: "darjeeling", before: row(100), after: row(200) },
      { id: "oolong", before: row(200), after: row(400) },
      { id: "sencha", before: row(300), after: row(600) },
    ], { width: 400, height: 800 });
    expect(result).toEqual({ value: 0.234375, impactFraction: 0.625, distanceFraction: 0.375, shifted: ["darjeeling", "oolong", "sencha"] });
    
  2. a point covered by several elements counts once, even where three overlap
    // Three cards drop into view from just above it; their new boxes overlap, all three in the middle.
    const result = layoutShift([
      { id: "green", before: { x: 0, y: -200, width: 200, height: 200 }, after: { x: 0, y: 0, width: 200, height: 200 } },
      { id: "mint", before: { x: 100, y: -200, width: 200, height: 200 }, after: { x: 100, y: 0, width: 200, height: 200 } },
      { id: "oolong", before: { x: 50, y: -200, width: 200, height: 200 }, after: { x: 50, y: 100, width: 200, height: 200 } },
    ], { width: 400, height: 800 });
    expect(result).toEqual({ value: 0.09375, impactFraction: 0.25, distanceFraction: 0.375, shifted: ["green", "mint", "oolong"] });
    
  3. the distance is the largest move of any element that counts, however small the element
    const result = layoutShift([
      { id: "banner", before: { x: 0, y: 0, width: 400, height: 400 }, after: { x: 0, y: 20, width: 400, height: 400 } },
      { id: "badge", before: { x: 20, y: 600, width: 40, height: 40 }, after: { x: 180, y: 600, width: 40, height: 40 } },
    ], { width: 400, height: 800 });
    expect(result.shifted).toEqual(["banner", "badge"]);
    expect(result.impactFraction).toBeCloseTo(0.535, 9);
    expect(result.distanceFraction).toBeCloseTo(0.2, 9);
    expect(result.value).toBeCloseTo(0.107, 9);
    
  4. an element that moved less than 3 px adds nothing
    const result = layoutShift([
      { id: "header", before: { x: 0, y: 0, width: 400, height: 300 }, after: { x: 0, y: 2, width: 400, height: 300 } },
      { id: "card", before: { x: 0, y: 400, width: 400, height: 100 }, after: { x: 0, y: 480, width: 400, height: 100 } },
    ], { width: 400, height: 800 });
    expect(result.shifted).toEqual(["card"]);
    expect(result.impactFraction).toBeCloseTo(0.225, 9);
    expect(result.distanceFraction).toBeCloseTo(0.1, 9);
    expect(result.value).toBeCloseTo(0.0225, 9);
    
  5. an element out of view in both frames doesn't count, not even its distance
    const result = layoutShift([
      { id: "footer", before: { x: 0, y: 1000, width: 400, height: 200 }, after: { x: 0, y: 1600, width: 400, height: 200 } },
      { id: "card", before: { x: 0, y: 100, width: 400, height: 100 }, after: { x: 0, y: 140, width: 400, height: 100 } },
    ], { width: 400, height: 800 });
    expect(result.shifted).toEqual(["card"]);
    expect(result.impactFraction).toBeCloseTo(0.175, 9);
    expect(result.distanceFraction).toBeCloseTo(0.05, 9);
    expect(result.value).toBeCloseTo(0.00875, 9);
    
  6. a slide that enters or leaves the view sideways doesn't count, one that also drops does
    const viewport = { width: 400, height: 800 };
    const slide = (x, y) => ({ x, y, width: 400, height: 200 });
    expect(layoutShift([
      { id: "next", before: slide(400, 300), after: slide(0, 300) },
      { id: "previous", before: slide(0, 300), after: slide(-400, 300) },
    ], viewport)).toEqual({ value: 0, impactFraction: 0, distanceFraction: 0, shifted: [] });
    expect(layoutShift([{ id: "next", before: slide(400, 300), after: slide(0, 310) }], viewport)).toEqual({ value: 0.125, impactFraction: 0.25, distanceFraction: 0.5, shifted: ["next"] });
    
  7. nothing that counts is a score of 0
    expect(layoutShift([], { width: 400, height: 800 })).toEqual({ value: 0, impactFraction: 0, distanceFraction: 0, shifted: [] });
    
  8. a grid of 60 cards that all drop 40 px
    const elements = [];
    for (let row = 0; row < 20; row++) {
      for (const x of [10, 140, 270]) {
        const box = { x, y: row * 80, width: 120, height: 60 };
        elements.push({ id: row + "-" + x, before: box, after: { ...box, y: box.y + 40 } });
      }
    }
    const result = layoutShift(elements, { width: 400, height: 800 });
    // Rows 0 to 9 are on screen; their boxes before and after touch, so each column is covered from 0 to 800.
    expect(result.shifted).toEqual(elements.slice(0, 30).map((element) => element.id));
    expect(result.impactFraction).toBeCloseTo(0.9, 9);
    expect(result.distanceFraction).toBeCloseTo(0.05, 9);
    expect(result.value).toBeCloseTo(0.045, 9);
    

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