Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Annotate our scroll events as passive #1684

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

rafaeelaudibert
Copy link
Member

Browsers need to process all events before they can do the default behavior. This is usually not a problem for most events because what we're doing is important and we can spend ~10ms processing an event, but this is a bigger problem when we're processing scroll events.

scroll events trigger hundreds of times per second, and if you spend 10ms processing it, scrolling becomes very slow. Luckily, browsers provide us a way to tell it: hey, I'm not going to call preventDefault, just scroll away! That's done by adding passive: true when calling addEventListener. This tells the browser we won't call it, and that if we do call it, it's ok, we're wrong and shouldn't have called it, you don't need to respect our call.

We are tracking scroll in two different places (should probably do only one, to be honest?) - dead clicks autocapture AND heatmaps (via scrollManager), so I've added it to both places!

It's hard to see performance improvements in my super power mega blaster M3 but it should be helpful for lower-end devices.

Browsers need to process all events before they can do the default behavior. This is usually not a problem for most events because what we're doing is important and we can spend ~10ms processing an event, but this is a bigger problem when we're processing `scroll` events.

`scroll` events trigger hundreds of times per second, and if you spend 10ms processing it, scrolling becomes very slow. Luckily, browsers provide us a way to tell it: hey, I'm not going to call `preventDefault`, just scroll away! That's done by adding `passive: true` when calling `addEventListener`. This basically tells the browser we won't call it, and that if we do call it, it's ok, we're wrong and shouldn't have called it, you don't need to respect our call.

We are tracking scroll in two different places (should probably do only one, to be honest?) - dead clicks autocapture AND heatmaps (via `scrollManager`), so I've added it to both places!

It's hard to see performance improvements in my super power mega blaster M3 but it should be helpful for lower-end devices.
@rafaeelaudibert rafaeelaudibert added the bump patch Bump patch version when this PR gets merged label Jan 23, 2025
@rafaeelaudibert rafaeelaudibert requested a review from a team January 23, 2025 22:45
Copy link

vercel bot commented Jan 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Jan 23, 2025 10:46pm

Copy link

Size Change: +736 B (+0.02%)

Total Size: 3.28 MB

Filename Size Change
dist/all-external-dependencies.js 209 kB +31 B (+0.01%)
dist/array.full.es5.js 266 kB +55 B (+0.02%)
dist/array.full.js 370 kB +86 B (+0.02%)
dist/array.full.no-external.js 369 kB +86 B (+0.02%)
dist/array.js 182 kB +55 B (+0.03%)
dist/array.no-external.js 180 kB +55 B (+0.03%)
dist/dead-clicks-autocapture.js 14.4 kB +31 B (+0.22%)
dist/main.js 182 kB +55 B (+0.03%)
dist/module.full.js 370 kB +86 B (+0.02%)
dist/module.full.no-external.js 369 kB +86 B (+0.02%)
dist/module.js 182 kB +55 B (+0.03%)
dist/module.no-external.js 180 kB +55 B (+0.03%)
ℹ️ View Unchanged
Filename Size
dist/customizations.full.js 13.8 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 116 kB
dist/recorder.js 116 kB
dist/surveys-preview.js 67.5 kB
dist/surveys.js 64.2 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

Copy link
Member

@robbie-c robbie-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need some kind of reassurance that this works on IE.

It's not supported (https://caniuse.com/?search=passive-event-listener) which is fine, but we need to make sure it doesn't crash

Edit: I meant to comment rather than approve. I promise I've used a computer before!

@robbie-c robbie-c requested review from robbie-c and a team January 23, 2025 22:52
@rafaeelaudibert
Copy link
Member Author

rafaeelaudibert commented Jan 23, 2025

Edit: I meant to comment rather than approve. I promise I've used a computer before!

Too late, I have infinite power now
image

but we need to make sure it doesn't crash

I'll see if my Windows 11 can download it :)

Edit: I can! https://github.com/ciao1092/IELauncher

@rafaeelaudibert
Copy link
Member Author

Copy link
Member

@robbie-c robbie-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@rafaeelaudibert rafaeelaudibert merged commit 96bce23 into main Jan 24, 2025
32 checks passed
@rafaeelaudibert rafaeelaudibert deleted the optimize-scroll-events branch January 24, 2025 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants