Skip to the code

Break down an interaction

INP tells the tea shop that tapping "Add to basket" is slow, but not why. Write interactionBreakdown(entry, events). entry is the event entry INP reports for the interaction (the first of web-vitals' metric.entries): startTime is when the shopper tapped, processingStart and processingEnd are when its event handlers started and finished, and startTime + duration is when the next frame was painted. events is every event entry the page observed, in the order they came, entry among them, and [entry] when it isn't given. Return { interactionTime, nextPaintTime, inputDelay, processingDuration, presentationDelay }, the way the attribution build of web-vitals 6.2.3 works them out.

First find the entries presented in the same frame as entry. A frame's render time is the startTime + duration of the entry that started it. Go through events in order. Searching from the newest frame back, an entry joins the first frame whose render time is within 8 ms of its own startTime + duration (exactly 8 ms apart still counts), and otherwise starts a new frame. Every handler in the frame delays its paint, so take the earliest processingStart and the latest processingEnd of the entries in entry's frame.

Then clamp the way web-vitals does. Processing starts at that earliest processingStart, but no earlier than entry.startTime: a handler that was still running when the shopper tapped isn't input delay. The next paint is at entry.startTime + entry.duration, but no earlier than processing starts, since duration is rounded to 8 ms and can land a little early. Processing ends at the latest processingEnd, but no later than the next paint. interactionTime is entry.startTime. Input delay runs from it to the start of processing, processing duration from the start of processing to its end, and presentation delay from there to the next paint.

const tap = { name: "pointerup", startTime: 1000, processingStart: 1030, processingEnd: 1060, duration: 200, interactionId: 7 };
const click = { name: "click", startTime: 1004, processingStart: 1060, processingEnd: 1150, duration: 192, interactionId: 7 };
interactionBreakdown(tap, [tap, click]);
// { interactionTime: 1000, nextPaintTime: 1200, inputDelay: 30, processingDuration: 120, presentationDelay: 50 }

What the tests check

  1. an entry on its own: the three parts add up to its duration
    const tap = { name: "click", entryType: "event", startTime: 2000, processingStart: 2040, processingEnd: 2160, duration: 200, interactionId: 3 };
    const parts = { interactionTime: 2000, nextPaintTime: 2200, inputDelay: 40, processingDuration: 120, presentationDelay: 40 };
    expect(interactionBreakdown(tap)).toEqual(parts);
    expect(interactionBreakdown(tap, [tap])).toEqual(parts);
    
  2. every handler in the entry's frame counts as processing
    const pointerup = { name: "pointerup", startTime: 1000, processingStart: 1030, processingEnd: 1060, duration: 200, interactionId: 7 };
    const click = { name: "click", startTime: 1004, processingStart: 1060, processingEnd: 1150, duration: 192, interactionId: 7 };
    expect(interactionBreakdown(pointerup, [pointerup, click])).toEqual({
      interactionTime: 1000,
      nextPaintTime: 1200,
      inputDelay: 30,
      processingDuration: 120,
      presentationDelay: 50,
    });
    
  3. a handler still running when the shopper tapped is processing, not input delay
    const hover = { name: "mouseover", startTime: 950, processingStart: 960, processingEnd: 1030, duration: 168, interactionId: 0 };
    const click = { name: "click", startTime: 1000, processingStart: 1030, processingEnd: 1060, duration: 120, interactionId: 8 };
    expect(interactionBreakdown(click, [hover, click])).toEqual({
      interactionTime: 1000,
      nextPaintTime: 1120,
      inputDelay: 0,
      processingDuration: 60,
      presentationDelay: 60,
    });
    
  4. entries 8 ms apart share a frame, and 9 ms apart don't
    const keydown = { name: "keydown", startTime: 1000, processingStart: 1010, processingEnd: 1050, duration: 200, interactionId: 12 };
    const keypress = { name: "keypress", startTime: 1000, processingStart: 1050, processingEnd: 1080, duration: 192, interactionId: 12 };
    const keyup = { name: "keyup", startTime: 1105, processingStart: 1180, processingEnd: 1205, duration: 104, interactionId: 12 };
    expect(interactionBreakdown(keydown, [keydown, keypress, keyup])).toEqual({
      interactionTime: 1000,
      nextPaintTime: 1200,
      inputDelay: 10,
      processingDuration: 70,
      presentationDelay: 120,
    });
    
  5. a frame keeps its first entry's render time, and an entry joins the newest frame close enough
    // Render times: 1000, 1007 (joins 1000), 1013 (13 from 1000: a new frame), 1006 (joins the newer 1013).
    const first = { name: "pointerdown", startTime: 904, processingStart: 910, processingEnd: 950, duration: 96, interactionId: 20 };
    const second = { name: "pointerover", startTime: 903, processingStart: 950, processingEnd: 990, duration: 104, interactionId: 0 };
    const third = { name: "click", startTime: 909, processingStart: 990, processingEnd: 1000, duration: 104, interactionId: 21 };
    const fourth = { name: "focusin", startTime: 910, processingStart: 1000, processingEnd: 1005, duration: 96, interactionId: 0 };
    const events = [first, second, third, fourth];
    expect(interactionBreakdown(third, events)).toEqual({
      interactionTime: 909,
      nextPaintTime: 1013,
      inputDelay: 81,
      processingDuration: 15,
      presentationDelay: 8,
    });
    expect(interactionBreakdown(first, events)).toEqual({
      interactionTime: 904,
      nextPaintTime: 1000,
      inputDelay: 6,
      processingDuration: 80,
      presentationDelay: 10,
    });
    
  6. the next paint is never before processing starts
    // No handlers, a long wait, and a duration rounded down to 96 ms.
    const click = { name: "click", startTime: 3000, processingStart: 3100, processingEnd: 3100, duration: 96, interactionId: 30 };
    expect(interactionBreakdown(click)).toEqual({
      interactionTime: 3000,
      nextPaintTime: 3100,
      inputDelay: 100,
      processingDuration: 0,
      presentationDelay: 0,
    });
    
  7. processing never runs past the next paint
    // A handler that showed alert(): processing ended long after the frame.
    const click = { name: "click", startTime: 4000, processingStart: 4020, processingEnd: 4300, duration: 104, interactionId: 31 };
    expect(interactionBreakdown(click)).toEqual({
      interactionTime: 4000,
      nextPaintTime: 4104,
      inputDelay: 20,
      processingDuration: 84,
      presentationDelay: 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).