Dynamic sizes lit demo crashes/ loads very slowly #1188
Describe the bug#
Your demo with the latest versions of lit-virtual (3.13.29) and virtual-core (3.17.0) either loads very slowly (> 1 minute) or just crashes the browser.
I was really happy a new version was available, because 3.16 (and some versions before) broke the scrollToIndex functionality in Lit, but this release might unfortunately be worse.
Once the page actually loads the scrollToIndex does seem to work 😁
Your minimal, reproducible example#
Steps to reproduce#
I isolated the RowVirtualizerDynamic scenario and removed the other 2 that were on the page.
There is no real reproduction path, just try to load the page.
It can take over a minute for the page to load sometimes. And sometimes the browser just crashes.
Expected behavior#
The page loads fast and the scrolling to halfway works.
How often does this bug happen?#
Often
Screenshots or Videos#
No response
Platform#
MacOS 26.5 Chrome 148.0.7778.215 Firefox 148.0.7778.215
tanstack-virtual version#
3.17.0
TypeScript version#
5.6.3
Additional context#
@tanstack/lit-virtual": "3.13.29"
I removed the generation of random paragraphs with faker and just have every item be the same string, that didn't solve anything.
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
@nikhilsdabhade I don't think faker itself is the problem, as i mentioned in my original post: I removed the generation of random paragraphs with faker and just have every item be the same string, that didn't solve anything.
While having a list of 10,000 items might be an edge case, it should be possible, and it did work in earlier versions.
@Diaan Yup, I replicated the issue and yes it's not working. Looking into it.
@Diaan @piecyk So what is happening is that if the
offsetHeightof the visible elements becomes 0px at any point (maybe due to display: none; check the example) then thecalculateRangetries to fill the container size (for e.g. 400px) it sees that the the actual consumed height is still 0px (because of the offsetHeight 0) and there's still space to fill so it tries to expand the elements and in this process all the elements are processed resulting in Maximum update depth exceeded error.Example with
react-virtual:- https://stackblitz.com/edit/vitejs-vite-idcjrgkr?file=src%2FApp.tsxOne solution i found is this
virtual-core/src/index.tsmeasureElement = (node: TItemElement | null) => { if ( (!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index) ) { const measuredSize = this.options.measureElement(node, undefined, this); // Avoid 0 size if (measuredSize > 0) { this.resizeItem(index, measuredSize); } } };This will prevent the caching 0px and will fallback to estimate size (for e.g. 45px)
@piecyk Should I open a PR to fix this?
@nikhilsdabhade Thanks for digging into this.
For this case we already have
useCachedMeasurements, which is intended to handle the situation where the whole list is temporarily hidden withdisplay: none.Docs: https://tanstack.com/virtual/latest/docs/api/virtualizer#usecachedmeasurements
When enabled, it falls back to the cached item size instead of updating measurements to
0px, so the virtualizer should not lose the previous measurements while the list is hidden.So I don’t think we should ignore all
0pxmeasurements globally inmeasureElement, because0can still be a valid measurement in some edge cases. For this specific issue, usinguseCachedMeasurementsshould be the right fix.Could you try the example with that option enabled and check if it resolves the maximum update depth issue?
@piecyk Yes adding
useCachedMeasurements: trueworked.But I still think it's an issue because the page itself becomes unresponsive due to all the items iterations. Not a good user experience, tbh.
@piecyk Yes adding
useCachedMeasurements: trueworked.But I still think it's an issue because the page itself becomes unresponsive due to all the items iterations. Not a good user experience, tbh.
Hmm, “all the item iterations”, can you explain what you mean by that? What is the use case?
@piecyk in
virtual-core/src/index.tsline no 1938while ( endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize ) { endIndex++ }When the items take up 0px, the getEnd(endIndex) function returns 0 and It increments endIndex to fetch more items to fill the void. In the end the loop iterates through all 10,000 items till
lastIndex.@piecyk in
virtual-core/src/index.tsline no 1938while ( endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize ) { endIndex++ } When the items take up 0px, the getEnd(endIndex) function returns 0 and It increments endIndex to fetch more items to fill the void. In the end the loop iterates through all 10,000 items till
lastIndex.@nikhilsdabhade Yeah, useCachedMeasurements should prevent DOM measurement and return either the cached size or estimateSize.
So the case I’m trying to understand is: how do we end up with all item sizes being 0 in the first place?
If useCachedMeasurements is enabled, measureElement should return:
instance.itemSizeCache.get(key) ?? instance.options.estimateSize(index)
So unless the cache already contains 0 for those items, or estimateSize(index) returns 0
@piecyk It works when the useCachedMeasurements is enabled.
I'm talking about the case where useCachedMeasurements is false (default state).
What i'm trying to say is that if the user have done
display: noneand by default useCachedMeasurements is false then as a user i'm not expecting the page to become unresponsive.There should be a way to avoid
Maximum update depth exceeded errorwhen useCachedMeasurements is false.I've submitted a PR (#1226) that addresses the performance issue with 0-size measurements in calculateRangeImpl.
Root cause: When elements are temporarily hidden (
display:none), ResizeObserver delivers size 0, which overwrites the valid cached measurement viaresizeItem. With all items at size 0,calculateRangeImpldetermines they are all in the visible range (since end = 0 for all items), causing all items to be rendered and freezing the browser.Fix:
resizeItemnow rejects 0-size measurements when the item already has a previously cached size. First-time measurements of genuinely 0-height items remain accepted since they have no prior cache entry.This approach is more targeted than the earlier proposal to globally reject all 0-size measurements. It:
- Preserves existing behavior for genuinely 0-height items on first measurement
- Prevents the infinite re-render when a previously-measured item gets a 0 size from
display:none - Works without requiring
useCachedMeasurements: true(though that option remains available)
@Diaan thanks for the report, and sorry for the slow reply.
This is fixed in
@tanstack/virtual-core3.18.0 and later, so please upgradelit-virtual/virtual-coreto the latest version.The cause: Lit's
refdirective callsmeasureElementbefore the row is attached to the document. A detached node has no layout, so its size was cached as 0. With every row at 0px, the range calculation pulled all 10,000 items into view and rendered them, and that froze the page. #1293 skips the sync measurement for nodes that are not connected yet. The ResizeObserver then reports the real size after the row is inserted.I checked this with the dynamic Lit example:
- core 3.17.0 – 3.17.11: the page hangs on load
- core 3.18.0 and later: the page loads in about 1s, and scroll to top / middle / end works
@nikhilsdabhade the
display: nonecase you found is a separate issue. For that one,useCachedMeasurements: trueis still the way to go.I'll close this one. If you still see it on the latest version, please reopen.
piecyk closed this
Sign in to join this conversation.
@Diaan I played around with the example and what i found is that this line is expensive
it tries to generate 10,000 sentences before the app renders, blocking the main thread.
If i just make it to 100 or 1000 then it works (all the scroll buttons are working properly)