As someone not very up-to-date in Web UI techniques, I was curious as to how the array was being advanced on scroll. Unfortunately, it looks like the answer is basically polling. The code constantly calls window.requestAnimationFrame(), checking window.pageYOffset. This means that the page is constantly doing work, even at idle. Surely the more correct way is to install an onscroll handler or something, right?
The correct way would be a mix of those two strategies: an onscroll handler that schedules an update through requestAnimationFrame (ensuring that it only gets scheduled once until the next frame update actually executes).
Request animation frame exists specifically to avoid ‘traditional’ polling. You’re thinking more of setInterval() which is the old way of doing it. The requestAnimationFrame docs are a good read, there are lots of performance benefits.
Comments (124) 49 threads
Show all comments
Showing thread 4 of 49
As someone not very up-to-date in Web UI techniques, I was curious as to how the array was being advanced on scroll. Unfortunately, it looks like the answer is basically polling. The code constantly calls window.requestAnimationFrame(), checking window.pageYOffset. This means that the page is constantly doing work, even at idle.
Surely the more correct way is to install an onscroll handler or something, right?
Continue this thread — 1 reply