bench(react-grab): event-listener pattern comparison (function-per-event vs handleEvent)#345
Draft
aidenybai wants to merge 3 commits into
Draft
bench(react-grab): event-listener pattern comparison (function-per-event vs handleEvent)#345aidenybai wants to merge 3 commits into
aidenybai wants to merge 3 commits into
Conversation
Adds a tiny utility that wraps a type-safe per-event-type handler map in a DOM EventListenerObject. This lets a single listener identity cover multiple event types via the native handleEvent dispatch, simplifying add/remove symmetry across viewport, drag, and dismiss listeners. Applies the pattern to: - register-overlay-dismiss (keydown / mousedown / touchstart) - create-toolbar-drag (pointermove / pointerup / pointercancel) - create-anchored-dropdown (viewport resize / scroll) - selection-label (window + visualViewport viewport listeners + keydown) - toolbar (window + visualViewport viewport listeners) Co-authored-by: Aiden Bai <aidenybai@users.noreply.github.com>
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
Headless-Chromium benchmark comparing function-per-event registration vs a single EventListenerObject with handleEvent across four representative scenarios (drag, viewport, dismiss, freeze-mouse). Run with: pnpm --filter react-grab bench:event-listener Co-authored-by: Aiden Bai <aidenybai@users.noreply.github.com>
…attern" This reverts commit f19eb71.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a headless-Chromium synthetic benchmark comparing the two ways of registering DOM event listeners:
EventListenerObject— a single object with ahandleEventmethod that dispatches onevent.type, reused across manyaddEventListenercalls.Why this PR exists
I originally adopted the
EventListenerObjectpattern across the codebase for ergonomic reasons. The bench shows it's actually a measurable perf regression on the dispatch hot path, so the call-site changes have been reverted. The bench itself is kept as durable infra so future "should we try this pattern" questions can be answered with numbers instead of vibes.Run it
Files:
packages/react-grab/bench/event-listener-pattern.html— self-contained bench pagepackages/react-grab/bench/run.mjs— Playwright/Chromium runner that prints results to stdoutpackages/react-grab/package.json— adds thebench:event-listenerscriptHeadline numbers
Headless Chromium 147 (Linux x86_64), median of 7 trials, positive delta =
handleEventslower.Dispatch (200,000 events)
EventListenerObjectAdd + remove (50,000 cycles)
EventListenerObjectPractical impact at our event rates
Per-event overhead of
handleEventis roughly(94.80 − 70.30) ms / 200,000 ≈ 120 ns/event. At a sustained 120 Hzpointermove/ scroll that's~14 µs/sec, well under 0.001% of a 16.67 ms frame — invisible in real UI. But there's no real-world scenario where it's faster, so the call-site adoption isn't worth keeping.pnpm typecheckandpnpm lintpass.Summary by cubic
Adopted the DOM
EventListenerObject(handleEvent) pattern inreact-grabvia a newcreateEventListenerutility and added a headless benchmark to compare it with per-event functions. Uses one listener identity for multiple event types to improve add/remove symmetry and type safety with no behavior changes.Refactors
utils/create-event-listener.tsto wrap a typed handler map into anEventListenerObject.register-overlay-dismiss(keydown, mousedown, touchstart; consistent capture).create-toolbar-drag(pointermove, pointerup, pointercancel withAbortController).create-anchored-dropdown(window/visualViewport resize, scroll).components/selection-label(window/visualViewport resize, scroll; keydown when expandable).components/toolbar(window/visualViewport resize, scroll).New Features
bench:event-listenerto compare function-per-event vshandleEventacross common scenarios. Run with: pnpm --filter react-grab bench:event-listener.Written for commit 4a9d0a3. Summary will update on new commits. Review in cubic