completefrontend

Predict the outputMedium

What INP do these interactions give?

One visit to the tea shop's menu produced these Event Timing entries: a tap, a hover, a key press, and then 59 quick taps. The code groups them into interactions, picks INP with the rule web-vitals 6.2.3 uses, and rates it. What does it log?

The code
// Event Timing entries from one visit to the tea shop's menu. duration runs from the input to the
// next paint after its handlers ran, in ms. Events with the same interactionId are one interaction;
// interactionId 0 means the event wasn't part of one.
const entries = [
  { name: "pointerdown", interactionId: 7001, duration: 120 }, // a tap on "Add to basket"
  { name: "pointerup", interactionId: 7001, duration: 152 },
  { name: "click", interactionId: 7001, duration: 152 },
  { name: "mouseover", interactionId: 0, duration: 600 }, // the pointer passing over the menu
  { name: "keydown", interactionId: 7008, duration: 520 }, // the first letter typed in the search box
  { name: "keyup", interactionId: 7008, duration: 64 },
];
// Then 59 quick taps on the quantity buttons.
for (let i = 0; i < 59; i++) {
  entries.push({ name: "click", interactionId: 7015 + 7 * i, duration: 24 + 8 * (i % 5) });
}
 
// An interaction's latency is the duration of its longest event.
const latencies = new Map();
for (const { interactionId, duration } of entries) {
  if (interactionId === 0) continue;
  latencies.set(interactionId, Math.max(latencies.get(interactionId) ?? 0, duration));
}
 
// web-vitals' rule: the longest interaction, skipping one for every 50 the page has had.
const longestFirst = [...latencies.values()].sort((a, b) => b - a);
const index = Math.min(longestFirst.length - 1, Math.floor(latencies.size / 50));
const inp = longestFirst[index];
const rating = inp > 500 ? "poor" : inp > 200 ? "needs-improvement" : "good";
 
console.log("Interactions: " + latencies.size);
console.log("Three longest: " + longestFirst.slice(0, 3).join(", ") + " ms");
console.log(`INP: ${inp} ms (${rating})`);

Practice it in the editor, with tests

The answer

Work out every line it logs, in order. Then check.

Checking your sign-in…