From 1337d73bac07b0fd83d54f65f0336ad99af62a2b Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Mon, 16 Nov 2026 00:37:59 -0500 Subject: [PATCH] Time keypress to paint by the browser's own measure where it has one Under a steady stream of keys, the task the page posts after each frame to note the time can wait behind many later frames: moving between forks on the large library recorded 400 ms while every key was painted in the next frame (the Event Timing API put each at 24 ms). Where the browser has measured a key's event to its next paint, that figure is the sample. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab --- src/ui/App.tsx | 2 +- src/ui/store.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ui/App.tsx b/src/ui/App.tsx index c328dde..220e7c3 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -497,7 +497,7 @@ export function App() { store.t0 = null; requestAnimationFrame(() => { const ch = new MessageChannel(); - ch.port1.onmessage = () => store.samples.push({ what: t.what, ms: performance.now() - t.at }); + ch.port1.onmessage = () => store.addSample(t, performance.now() - t.at); ch.port2.postMessage(0); }); } diff --git a/src/ui/store.ts b/src/ui/store.ts index d77f290..381f9f0 100644 --- a/src/ui/store.ts +++ b/src/ui/store.ts @@ -145,6 +145,8 @@ export interface Pending { export interface Sample { what: string; ms: number; + /** The key's time stamp, to match the browser's own measure of it. */ + at?: number; } /** A replica of a document from before the library, holding edits the server never received. */ @@ -245,6 +247,37 @@ class Store { /** Whether the window is wide enough for `auto` to mean panes. Kept current by the page. */ wide = typeof window !== "undefined" && window.innerWidth >= PANES_MIN_WIDTH; samples: Sample[] = []; + /** + * Keypress → paint, as the browser itself measures it (the Event Timing API: from a key's event to + * the next paint after its handlers), by the key's time stamp. Under a steady stream of keys an + * ordinary task posted after a frame can wait behind many later frames, so where the browser has + * measured a key, its figure is the sample's. + */ + private painted = new Map(); + private paintObserver = (() => { + try { + const o = new PerformanceObserver((list) => { + for (const e of list.getEntries()) if (e.name === "keydown") this.notePaint(e.startTime, e.duration); + }); + o.observe({ type: "event", durationThreshold: 16 } as PerformanceObserverInit); + return o; + } catch { + return null; + } + })(); + private notePaint(at: number, ms: number) { + const s = this.samples.find((x) => x.at !== undefined && Math.abs(x.at - at) < 1); + if (s) s.ms = Math.min(s.ms, ms); + else this.painted.set(at, ms); + } + /** Record a keypress → paint sample (App.tsx), taking the browser's own figure for it where there is one. */ + addSample(t: { what: string; at: number }, ms: number) { + for (const r of this.paintObserver?.takeRecords() ?? []) if (r.name === "keydown") this.notePaint(r.startTime, r.duration); + const by = [...this.painted].find(([at]) => Math.abs(at - t.at) < 1); + if (by) this.painted.delete(by[0]); + this.samples.push({ what: t.what, ms: by ? Math.min(ms, by[1]) : ms, at: t.at }); + if (this.painted.size > 200) this.painted.clear(); + } /** Set at a keypress; cleared when the resulting frame has been painted. */ t0: { what: string; at: number } | null = null; /** Replicas of pre-library documents whose edits never reached the server. */