-
Notifications
You must be signed in to change notification settings - Fork 19
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
Question regarding CPU usage when idle #68
Comments
Hi, @kiejo I think intersection observer for spacers (a div which fills virtualized space) can be used and I made some drafts with it a time ago. Mostly it works fine. I want to move to this solution in future if I'll make it good in all cases. I need a way to know when elements changed their positions near viewport - it can be scroll, parent resize, shifts by elements added on top/bottom, window resize, item height change... Endless request animation frame handle all of these "events". |
Thanks for the quick answer and explanation! Using an intersection observer that spans the list's viewport sounds like a good potential approach to detect when elements move out of or into the viewport. I will have to look at the current implementation in more detail to better understand how everything works at the moment. |
@kiejo I can explain current logic deeply. Every frame I compute rects of spacers, first/last rendered list items and viewport bounds. Then I check is everything shown inside viewport without free spaces. If I need to render more items, I change "indexes to render", but it's some kind of estimation - at next frame indexes may be corrected. Logic now: render some items -> check that all items fill viewport -> correct indexes to fill viewport. When you scroll last item can be fully intersect the viewport - this is the moment I change indexes for example. |
That's an interesting approach. It sounds like an I haven't tried any of this, but in theory I think this could potentially handle cases like scrolling, parent/window resize, new elements being added within the rendered range, and height changes of rendered items. It sounds like you may have already thought about something like this as you mentioned you already made some drafts using |
@kiejo Yes, I made exactly you described with Anyway I think intersection observer is a good way to "make it right" without hacky rAF. |
Good to hear that you already tried this approach :)
Maybe something like this could help with this case: function update() {
// Needs to stop the update loop when the start/end of the items array is reached and the
// items do not take enough space to push the spacer element out of the list rect (prevent
// endless update loop).
}
let frameId = null
function updateLoop() {
frameId = requestAnimationFrame(updateLoop)
update()
}
function startUpdateLoop() {
if (!frameId)
updateLoop()
}
function stopUpdateLoop() {
if (frameId) {
cancelAnimationFrame(frameId)
frameId = null
}
}
const observer = new IntersectionObserver(entries => {
if (entries[0].isIntersecting)
startUpdateLoop()
else
stopUpdateLoop()
}, {
root: listElement,
threshold: 0,
})
observer.observe(spacerElement) This is just some ideation on how this could potentially work and I'm sure the logic would need to handle more cases. |
@kiejo , good suggestion (thought about it, but not tried) Also different threshold of intersection can be used with really small steps - like [0.001, 0.002..., 1] to catch shifts/changes in intersection - but intersection handler can evaluate frequently and not cover cases where 0.001 is hundreds of pixels. |
I'm currently looking at different virtualization libraries and so far really like this one. But after experimenting a little more with it, I noticed that there is always some CPU usage going on even when nothing is being scrolled or changed. I noticed that this is caused by
requestAnimationframe
continuously firing and doing work.Now my question is whether this library could be optimized to skip this work when nothing is changing or whether this is somehow inherent to the technical approach of this library. I haven't looked into the implementation details yet, but haven't run into the same issue with other virtualization libraries.
The text was updated successfully, but these errors were encountered: