Skip to content

Commit fa67e7e

Browse files
committed
fix: count every interaction for the INP percentile
The 98th percentile skipped one interaction per 50 of those kept in the map, which holds only interactions of 40 ms or more and is trimmed past 200, so INP came out worse than the browser's. It now counts performance.interactionCount where the browser has it, and otherwise every new interaction id, which trimming does not reset. Tests cover fast interactions and a trimmed map.
1 parent b5ac4f7 commit fa67e7e

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎packages/devtools/src/__tests__/perf-collector.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,37 @@ describe('attachPerformance', () => {
143143
expect(perf.report().metrics.INP!.value).toBe(159);
144144
});
145145

146+
it('counts fast interactions the browser reports but the 40 ms observer never sends', () => {
147+
const { env, emit } = fakeEnv(ALL);
148+
// 10 slow interactions observed, 140 in all: skip the slowest 2 of them.
149+
env.interactionCount = () => 140;
150+
const perf = attachPerformance(rpc().my, 'tab1', () => undefined, { env });
151+
emit(
152+
'event',
153+
Array.from({ length: 10 }, (_, i) => ({
154+
name: 'click',
155+
interactionId: i + 1,
156+
duration: 100 + i,
157+
})),
158+
);
159+
expect(perf.report().metrics.INP!.value).toBe(107);
160+
});
161+
162+
it('keeps counting interactions after the map is trimmed', () => {
163+
const { env, emit } = fakeEnv(ALL);
164+
const perf = attachPerformance(rpc().my, 'tab1', () => undefined, { env });
165+
// 250 interactions, so the map keeps only the slowest 100: still skip 5.
166+
emit(
167+
'event',
168+
Array.from({ length: 250 }, (_, i) => ({
169+
name: 'click',
170+
interactionId: i + 1,
171+
duration: 40 + i,
172+
})),
173+
);
174+
expect(perf.report().metrics.INP!.value).toBe(284);
175+
});
176+
146177
it('counts long tasks and their blocking time, and prefers long animation frames', () => {
147178
const tasks = fakeEnv(ALL);
148179
const perf = attachPerformance(rpc().my, 'tab1', () => undefined, { env: tasks.env });

‎packages/devtools/src/perf-collector.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ export interface PerfEnv {
115115
observe(type: string, onEntries: (entries: PerfEntry[]) => void): (() => void) | null;
116116
/** Entries the browser already has, such as the navigation entry. */
117117
entries(type: string): PerfEntry[];
118+
/**
119+
* Every interaction on the page so far, fast ones included
120+
* (`performance.interactionCount`), where the browser counts them.
121+
*/
122+
interactionCount?(): number | undefined;
118123
}
119124

120125
const round = (value: number, digits = 0) => {
@@ -124,6 +129,10 @@ const round = (value: number, digits = 0) => {
124129

125130
function browserEnv(): PerfEnv {
126131
return {
132+
interactionCount() {
133+
const count = (performance as { interactionCount?: unknown }).interactionCount;
134+
return typeof count === 'number' && Number.isFinite(count) ? count : undefined;
135+
},
127136
observe(type, onEntries) {
128137
if (typeof PerformanceObserver === 'undefined') return null;
129138
const types = (PerformanceObserver as { supportedEntryTypes?: readonly string[] })
@@ -190,6 +199,9 @@ export function attachPerformance(
190199
let sessionStart = -Infinity;
191200
let lastShift = -Infinity;
192201
const interactions = new Map<number, PerfInteraction>();
202+
/** Interactions seen so far, kept when the map is trimmed; ids only grow. */
203+
let interactionsSeen = 0;
204+
let highestInteractionId = 0;
193205
const longTasks: PerfLongTask[] = [];
194206
let longTaskCount = 0;
195207
let totalBlockingMs = 0;
@@ -249,6 +261,10 @@ export function attachPerformance(
249261
event: (entries) => {
250262
for (const entry of entries) {
251263
if (!entry.interactionId) continue;
264+
if (entry.interactionId > highestInteractionId) {
265+
highestInteractionId = entry.interactionId;
266+
interactionsSeen++;
267+
}
252268
const known = interactions.get(entry.interactionId);
253269
if (known && known.durationMs >= entry.duration) continue;
254270
const target = describeElement(entry.target);
@@ -317,8 +333,10 @@ export function attachPerformance(
317333
const inp = (): PerfMetric | undefined => {
318334
const all = [...interactions.values()].sort((a, b) => b.durationMs - a.durationMs);
319335
if (!all.length) return undefined;
320-
// The 98th percentile: skip one of the slowest per 50 interactions.
321-
const worst = all[Math.min(all.length - 1, Math.floor(interactions.size / 50))]!;
336+
// The 98th percentile: skip one of the slowest per 50 interactions, counting
337+
// every interaction, not only the slow ones the map keeps.
338+
const total = Math.max(env.interactionCount?.() ?? 0, interactionsSeen);
339+
const worst = all[Math.min(all.length - 1, Math.floor(total / 50))]!;
322340
return {
323341
value: worst.durationMs,
324342
rating: perfRating('INP', worst.durationMs),

0 commit comments

Comments
 (0)