Write onINP(report), which measures Interaction to Next Paint on a live page and reports it as onINP in web-vitals 6.2.3 does. The rules for picking INP are the inp-from-event-timing problem's; here you feed them from the browser and decide when to report.
Watch two entry types, both with buffered: true: event entries, observed with durationThreshold: 40, so the browser sends every event from 40 ms up rather than from its default of 104 ms; and first-input, which the browser sends for the page's first interaction however quick it was, so a page where every tap was fast still has an INP. Do nothing at all where PerformanceObserver.supportedEntryTypes doesn't list "event".
Handle each entry as it arrives. Skip one whose interactionId is 0. Entries with the same interactionId are one interaction, whose latency is its longest entry's duration; its entries are the first entry with that duration plus any later one with the same duration and the same startTime. Keep the 10 longest interactions, longest first: a new interaction gets in while there are fewer than 10, or when it's longer than the shortest one kept, which then drops out. Of two with the same latency, the one that reached it first ranks higher.
INP is the interaction at index Math.floor(count / 50) of that list, or the last one in it when the list is shorter, where count is performance.interactionCount: the number of interactions the page has had, quick ones included. Use 0 where the browser doesn't have it (web-vitals estimates it there, from the interactionIds it sees).
Report when the page is hidden, on a visibilitychange that finds document.visibilityState is "hidden". First handle the observer's takeRecords(), for entries the browser hasn't delivered yet. Then, if there is an INP, call report with a plain object { name: "INP", value, delta, entries }: its latency, the change since the last report (the whole value the first time), and its entries. A later hide reports only if the value changed. With no interaction, report nothing.
web-vitals handles entries in an idle callback, to give an interaction's last entries time to arrive, and starts over after a back/forward cache restore. Leave both out.
onINP((metric) => console.log(metric.value, metric.entries.map((entry) => entry.name)));
// A tap on "Add to basket": pointerdown 48 ms, pointerup 64 ms, click 56 ms.
// Typing "oolong" in the search box: a keydown of 88 ms.
// The reader switches tabs: 88 ["keydown"]
const perf = usePerformance();
onINP(() => {});
const observers = perf.observers();
expect(observers.some((observer) => observer.types.includes("event") && observer.buffered && observer.durationThreshold === 40)).toBe(true);
expect(observers.some((observer) => observer.types.includes("first-input") && observer.buffered)).toBe(true);
const perf = usePerformance();
const event = (name, startTime, duration, interactionId) => ({ name, startTime, duration, interactionId });
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
perf.emit("event", [event("pointerdown", 1200, 48, 7001), event("pointerup", 1290, 64, 7001), event("click", 1296, 56, 7001)]);
const typing = perf.emit("event", [event("keydown", 5000, 88, 7008)]);
await flush();
expect(reports).toEqual([]);
setVisibility("hidden");
expect(reports).toEqual([{ name: "INP", value: 88, delta: 88, entries: typing }]);
const perf = usePerformance();
const event = (name, startTime, duration, interactionId) => ({ name, startTime, duration, interactionId });
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
perf.emit("event", [event("mouseover", 900, 160, 0)]);
const tap = perf.emit("event", [event("pointerdown", 3000, 56, 7015), event("pointerup", 3100, 88, 7015), event("click", 3100, 88, 7015)]);
perf.emit("event", [event("keydown", 5000, 72, 7022), event("keyup", 5080, 72, 7022)]);
setVisibility("hidden");
expect(reports).toEqual([{ name: "INP", value: 88, delta: 88, entries: [tap[1], tap[2]] }]);
const perf = usePerformance();
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
perf.emit("event", [{ name: "pointerdown", startTime: 700, duration: 24, interactionId: 7001 }]);
const [first] = perf.emit("first-input", [{ name: "pointerdown", startTime: 700, duration: 24, interactionId: 7001 }]);
setVisibility("hidden");
expect(reports).toEqual([{ name: "INP", value: 24, delta: 24, entries: [first] }]);
const perf = usePerformance();
perf.buffer("first-input", [{ name: "keydown", startTime: 300, duration: 48, interactionId: 7001 }]);
const [slow] = perf.buffer("event", [{ name: "click", startTime: 900, duration: 136, interactionId: 7008 }]);
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
setVisibility("hidden");
expect(reports).toEqual([{ name: "INP", value: 136, delta: 136, entries: [slow] }]);
await flush();
setVisibility("visible");
setVisibility("hidden");
expect(reports).toHaveLength(1);
const perf = usePerformance();
let count = 0;
Object.defineProperty(performance, "interactionCount", { configurable: true, get: () => count });
const event = (name, startTime, duration, interactionId) => ({ name, startTime, duration, interactionId });
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
const entries = perf.emit("event", [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)]);
count = 5;
setVisibility("hidden");
setVisibility("visible");
count = 120; // many quick interactions, none of them slow enough to be an entry
setVisibility("hidden");
expect(reports).toEqual([
{ name: "INP", value: 400, delta: 400, entries: [entries[0]] },
{ name: "INP", value: 200, delta: -200, entries: [entries[2]] },
]);
const perf = usePerformance();
Object.defineProperty(performance, "interactionCount", { configurable: true, get: () => 600 });
const latencies = [152, 208, 104, 184, 136, 200, 112, 176, 192, 120, 168, 144];
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
const entries = perf.emit("event", latencies.map((duration, index) => ({ name: "click", startTime: 1000 * (index + 1), duration, interactionId: 7001 + 7 * index })));
setVisibility("hidden");
expect(reports).toEqual([{ name: "INP", value: 120, delta: 120, entries: [entries[9]] }]);
const perf = usePerformance();
const event = (name, startTime, duration, interactionId) => ({ name, startTime, duration, interactionId });
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
setVisibility("hidden");
expect(reports).toEqual([]);
setVisibility("visible");
const tap = perf.emit("event", [event("click", 2000, 200, 7001)]);
setVisibility("hidden");
setVisibility("visible");
setVisibility("hidden");
setVisibility("visible");
perf.emit("event", [event("keydown", 4000, 96, 7008)]); // shorter: INP stays 200
setVisibility("hidden");
setVisibility("visible");
const filter = perf.emit("event", [event("click", 6000, 312, 7015)]);
setVisibility("hidden");
expect(reports).toEqual([
{ name: "INP", value: 200, delta: 200, entries: tap },
{ name: "INP", value: 312, delta: 112, entries: filter },
]);
const perf = usePerformance();
perf.supported(["first-input", "largest-contentful-paint", "layout-shift", "mark", "measure", "navigation", "paint", "resource"]);
const reports = [];
onINP((metric) => reports.push({ ...metric, entries: [...metric.entries] }));
expect(perf.observers()).toEqual([]);
setVisibility("hidden");
expect(reports).toEqual([]);
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).
I'm here to help with Observe INP. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.