Skip to content

perf(scroll-state): remove AnimationFrames loop#410

Open
fvarano wants to merge 2 commits intosvecosystem:mainfrom
fvarano:perf/optimize-scroll-state
Open

perf(scroll-state): remove AnimationFrames loop#410
fvarano wants to merge 2 commits intosvecosystem:mainfrom
fvarano:perf/optimize-scroll-state

Conversation

@fvarano
Copy link

@fvarano fvarano commented Feb 27, 2026

This utility uses requestAnimationFrame to continuously update arrivedState, which has quite a big overhead.

This PR removes continuous requestAnimationFrame loop that caused forced reflows by calling getComputedStyle() every frame. Replace with opt-in MutationObserver for dynamic content scenarios.

The original useScroll utility from Vue does not, instead it provides an optional observe option.

These are (naive) profiles on the docs page of this utility, which are slightly skewed to the old version, since that one is on a production build, while the website hosted locally is not:

Before:
image

After:
image

Cannot run tests locally, at the moment, but this utility does not have specific tests.

Pull request checklist

  • Code is formatted and linted (pnpm lint, pnpm format)
  • Types pass (pnpm check)
  • Tests pass locally (pnpm test)
  • Docs updated if applicable (content + demo)

…nObserver

Remove continuous requestAnimationFrame loop that caused forced reflows
by calling getComputedStyle() every frame. Replace with opt-in
MutationObserver for dynamic content scenarios.

- Remove AnimationFrames loop
- Add observe option (default: false) for DOM change detection
- Align with VueUse's useScroll pattern
- Fixes performance bottleneck visible in Chrome DevTools profiler
Add documentation for the new observe option that enables MutationObserver for detecting DOM changes affecting scroll state.
@changeset-bot
Copy link

changeset-bot bot commented Feb 27, 2026

⚠️ No Changeset found

Latest commit: 9be7924

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@kraktus
Copy link

kraktus commented Feb 27, 2026

Cannot run tests locally, at the moment, but this utility does not have specific tests.

FYI, it's most probably due to nodejs 25, had the same issue

for runed/ contribution, tests failing on node25
vitest-dev/vitest#8757
nodejs/node#60303

using nodev24 resolved it, but as you said it's not important if you don't add tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants