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

An unnecessary full style recalculation is done every scroll event on many pages of colorjs.io #587

Open
Inwerpsel opened this issue Aug 23, 2024 · 1 comment
Assignees

Comments

@Inwerpsel
Copy link

Inwerpsel commented Aug 23, 2024

When checking dev tools for the site, I noticed that every scroll event the --scrolltop custom property of the html element is updated.

While at a glance this might not look expensive, it actually results in a style recalculation of all elements on the page. I'm pretty sure it's the only reason the scrolling is choppy, and removing this property update is enough to make the page scroll normally.

Performance recording during scrolling

Details of a single recalc, taking 25.18ms on desktop.

colorjs-scrolling-recalc-detail

This is triggered by an event listener that only sets a custom property on the root element.

document.addEventListener("scroll", evt => {
root.style.setProperty("--scrolltop", root.scrollTop);
}, {passive: true});

The only reference to this custom property is in the #toc element, where it's used to move a position: fixed element depending on the scroll position.

@media (min-width: 1480px) {
position: fixed;
top: 11rem;
right: calc(var(--page-width) + var(--page-margin) + 1em);
width: fit-content;
max-width: calc(var(--page-margin) - 1em);
margin-left: 1em;
@supports (top: max(1em, 1px)) {
top: max(0em, 11rem - var(--scrolltop) * 1px);
}
}

It looks a lot like this CSS can be replaced with position: sticky, completely removing the need to update the root property and thereby removing all the time spent on the purple stuff in above graphs.

@Inwerpsel Inwerpsel changed the title A full style recalculation is done every scroll on many pages of colorjs.io An unnecessary full style recalculation is done every scroll on many pages of colorjs.io Aug 25, 2024
@Inwerpsel Inwerpsel changed the title An unnecessary full style recalculation is done every scroll on many pages of colorjs.io An unnecessary full style recalculation is done every scroll event on many pages of colorjs.io Aug 25, 2024
@svgeesus
Copy link
Member

Scrolling is indeed choppy; feel free to send a pull request!

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

No branches or pull requests

3 participants