Skip to the code

INP from Event Timing entries

Interaction to Next Paint says how long the page took to respond to the user. Write inp(entries, interactionCount) that computes it from Event Timing entries the way web-vitals 6.2.3 does. entries are the event entries the page's observer got, in order: objects with name (the event's type), startTime, duration (from the input until the next paint after its handlers ran, in ms, which the browser rounds to 8), and interactionId. interactionCount is performance.interactionCount, the number of interactions the page has had. It counts the quick ones too: an observer gets no entry for an event shorter than its durationThreshold. Return { value, entries }, or null when no entry belongs to an interaction.

An entry whose interactionId is 0 isn't part of an interaction (a mouseover, say) and is skipped. Entries with the same interactionId are one interaction: a tap gives a pointerdown, a pointerup, and a click, and a key press a keydown and a keyup. An interaction's latency is its longest entry's duration, and its entries are the first entry with that duration plus any later one with the same duration and the same startTime.

web-vitals keeps the 10 longest interactions, longest first. When two have the same latency, the one that reached it first ranks higher, so an interaction that only ties the 10th doesn't get in. INP is the interaction at index Math.floor(interactionCount / 50) of that list, or the last one in it when the list is shorter: the worst interaction while the page has had fewer than 50, and one more skipped for every 50 after that. value is its latency, and entries are its entries.

inp([
  { name: "pointerdown", startTime: 1200, duration: 48, interactionId: 7001 }, // a tap on "Add to basket"
  { name: "pointerup", startTime: 1290, duration: 64, interactionId: 7001 },
  { name: "click", startTime: 1290, duration: 64, interactionId: 7001 },
  { name: "mouseover", startTime: 3010, duration: 104, interactionId: 0 }, // not an interaction
  { name: "keydown", startTime: 5000, duration: 232, interactionId: 7008 }, // typing "oolong" in the search box
], 2);
// { value: 232, entries: [the keydown] }

What the tests check

  1. no interactions is null, and entries with an interactionId of 0 don't count
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    expect(inp([], 0)).toBe(null);
    expect(inp([event("mouseover", 400, 160, 0), event("pointermove", 900, 120, 0)], 0)).toBe(null);
    
  2. an interaction's latency is its longest entry
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    const tap = [event("pointerdown", 1200, 48, 7001), event("pointerup", 1290, 64, 7001), event("click", 1296, 120, 7001)];
    const result = inp([event("mouseover", 1100, 136, 0), ...tap], 1);
    expect(result).toEqual({ value: 120, entries: [tap[2]] });
    expect(result.entries[0]).toBe(tap[2]);
    
  3. entries that tie for the longest join only when they start at the same time
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    const tap = [event("pointerdown", 3000, 56, 7015), event("pointerup", 3100, 88, 7015), event("click", 3100, 88, 7015)];
    expect(inp(tap, 1)).toEqual({ value: 88, entries: [tap[1], tap[2]] });
    const press = [event("keydown", 5000, 96, 7022), event("keyup", 5080, 96, 7022)];
    expect(inp(press, 1)).toEqual({ value: 96, entries: [press[0]] });
    
  4. INP is the worst interaction while there are fewer than 50
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    const entries = [event("click", 1000, 400, 7001), event("keydown", 2000, 88, 7008), event("click", 3000, 200, 7015), event("keydown", 4000, 56, 7022), event("click", 5000, 312, 7029)];
    expect(inp(entries, 5)).toEqual({ value: 400, entries: [entries[0]] });
    expect(inp(entries, 49).value).toBe(400);
    
  5. skips one interaction for every 50 the page has had
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    const entries = [event("click", 1000, 400, 7001), event("keydown", 2000, 88, 7008), event("click", 3000, 200, 7015), event("keydown", 4000, 56, 7022), event("click", 5000, 312, 7029)];
    expect(inp(entries, 50)).toEqual({ value: 312, entries: [entries[4]] });
    expect(inp(entries, 120).value).toBe(200);
    expect(inp(entries, 199).value).toBe(88);
    
  6. when the count points past the list, INP is the shortest one in it
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    // 400 interactions, but only three were slow enough to give entries.
    const entries = [event("click", 1000, 160, 7001), event("keydown", 2000, 48, 7008), event("click", 3000, 104, 7015)];
    expect(inp(entries, 400)).toEqual({ value: 48, entries: [entries[1]] });
    expect(inp(entries, 250).value).toBe(48);
    
  7. only the 10 longest are kept, so a busy page reports the 10th longest
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    const latencies = [152, 208, 104, 184, 136, 200, 112, 176, 192, 120, 168, 144];
    const entries = latencies.map((duration, index) => event("click", 1000 * (index + 1), duration, 7001 + 7 * index));
    expect(inp(entries, 400).value).toBe(136);
    expect(inp(entries, 600)).toEqual({ value: 120, entries: [entries[9]] });
    expect(inp(entries, 5000).value).toBe(120);
    
  8. of two interactions with the same latency, the one that reached it first ranks higher
    const event = (name, startTime, duration, interactionId) => ({ entryType: "event", name, startTime, duration, interactionId });
    const press = event("pointerdown", 1000, 104, 7001); // a long press on a tea photo starts
    const key = event("keydown", 2000, 200, 7008); // a key press reaches 200 ms first
    const release = event("click", 3000, 200, 7001); // the long press reaches 200 ms when it ends
    expect(inp([press, key, release], 0)).toEqual({ value: 200, entries: [key] });
    expect(inp([press, key, release], 50)).toEqual({ value: 200, entries: [release] });
    

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