Replies: 2 comments 8 replies
-
Looks like good idea 👍 Just curious when do you memo the item? |
Beta Was this translation helpful? Give feedback.
-
I believe I discovered almost the same workaround when I was implementing a virtual grid with lots of elements. Here is a playful DEMO. Notice the red border highlighting the rows/cols ranges in the top right corner each time when any of them is changing (== react render). The cells have a hover state, so you can notice how performance degrades for a second each time when a new line appears on the screen. The roots of the implementation go to a Dynamic array (wiki) solution, where an array expands twice in size as soon as it reaches maximum capacity, and shrink twice in size as soon as there is less than a quarter of capacity. Imagine a static array, like in Java with 6 elements, its capacity will be 2³ = 8: ["1", "2", "3", "4", "5", "6", null, null]
^-----^----- placeholders The array keeps the same when the 7th element pushes into it: ["1", "2", "3", "4", "5", "6", "7", null] But when it pushes the 8th element, it creates a new twice longer array, and copies all elements into it: ["1", "2", "3", "4", "5", "6", "7", "8", null, null, null, null, null, null, null, null] From now and until the 16th element it pushes into the array with a constant time O(1). But if it pops the 8th item the array does not shrink twice, because in this case it might cause too much back and forth if it needs to push and pop the "edge" element. ["1", "2", "3", "4", "5", "6", "7", null, null, null, null, null, null, null, null, null] So instead the array will shrink twice as soon as there is a quarter capacity full: ["1", "2", "3", "4", null, null, null, null] It should shrink twice (not four times) in this case, to prevent immediate expansion on the following push: ["1", "2", "3", "4", "5", null, null, null] You might notice that this is a pretty smart and simple algorithm to implement a dynamic array with a static one under the hood. The only price we pay is space O(n). The virtual scrolling might do the same trick but for starting and ending points of a range: it should expand In my opinion, this is a low-hanging fruit to improve the library's performance for all of the current users (bug-fix change) because none of the exposing API changes. In the worst case, a library user might override the I am not sure I see which memorization @olee mentioned in the idea description but I didn't get any extra re-renders when the custom range is applied. @piecyk let me know if that makes sense at all, I'd be happy to contribute 🙏 |
Beta Was this translation helpful? Give feedback.
-
I tried to optimize the scroll performance in my list by only updating the rendered items in small "batches" by using this range extractor:
However, when investigating why it was still rendering my component all the time, I noticed that the result of the range extractor is not actually properly memoized.
The following lines from here should imho be changed from
to
This would allow reducing the amount of DOM updates and could improve performance in some scenarios.
Beta Was this translation helpful? Give feedback.
All reactions