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

Issue with "Document: scroll event": Example using rAF in scroll-event callbacks is outdated, and anti-performance #12701

Open
hiikezoe opened this issue Feb 4, 2022 · 3 comments
Labels
area: DOM/CSSOM Content:WebAPI Web API docs effort: small This task is a small effort. help wanted If you know something about this topic, we would love your help!

Comments

@hiikezoe
Copy link

hiikezoe commented Feb 4, 2022

MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/Document/scroll_event

What information was incorrect, unhelpful, or incomplete?

The example using requestAnimationFrame in scroll event callbacks is totally outdated, it in fact anti-performance wise in these days.

Firing scroll events is aligned/defined in the HTML spec, all major browsers, Chrome, Safari and Firefox fire before processing requestAnimationFrame callbacks, so with the requestAnimationFrame callacks it just triggers queuing a redundant requestAnimationFrame callback and invoking the callback.

Specific section or headline?

https://developer.mozilla.org/en-US/docs/Web/API/Document/scroll_event#examples

What did you expect to see?

Scroll event handlers can be used without requestAnimationFrame callbacks.

Did you test this? If so, how?

I tested with a simple example having scroll event handler and requestAnimationFrame callbacks something like this;

window.addEventListener("scroll", e => {                                        
  console.log("got a scroll event on " + document.timeline.currentTime);        
});                                                                             
requestAnimationFrame(function rAF() {                                          
  console.log("got a rAF callback on " + document.timeline.currentTime);        
  requestAnimationFrame(rAF);                                                   
});

Thank you, MDN content editors.

MDN Content page report details
@github-actions github-actions bot added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 4, 2022
@hiikezoe
Copy link
Author

hiikezoe commented Feb 4, 2022

One of my colleague @mstange realized me that the example might want to use a single requestAnimationFrame in a different way. Something like;

var registered_raf = false;
function ensure_raf() {
  if (!registered_raf) {
    window.requestAnimationFrame(() => {
      // all stuff mutating the DOM tree here.
      registered_raf = false;
    });
    registered_raf = true;
  }
}
window.addEventListener("scroll", ensure_raf);
visualViewport.addEventListener("scroll", ensure_raf);

This would actually result good performance rather than mutating DOM respectively in each callback.

I haven't run the above script, so there may be typos or some such. :)

@hiikezoe
Copy link
Author

hiikezoe commented Feb 4, 2022

after looking at the example carefully, it looks like it wanted to do the same thing what I comment. :)

A big difference is that the example register the rAF callback inside the scroll event handler, I think it should be outside of the handler so that it's less confusing for web developers.

@sideshowbarker sideshowbarker added Content:WebAPI Web API docs help wanted If you know something about this topic, we would love your help! effort: small This task is a small effort. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Feb 4, 2022
@sideshowbarker sideshowbarker changed the title Issue with "Document: scroll event": (short summary here please) Feb 4, 2022
@Josh-Cena
Copy link
Member

Related: #10580, #18843

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: DOM/CSSOM Content:WebAPI Web API docs effort: small This task is a small effort. help wanted If you know something about this topic, we would love your help!
3 participants