ui/ux
list virtualization
Mar 2025
In my example I have a total of 50 items in the list and here is the comparison of the number of total html elements in the DOM. Without virtualization, it has 197 nodes. With virtualization it has 27 nodes. This is an 86% reduction in the DOM nodes. This factor can change based on the elements each list item has. Virtualis. helps in reducing the number of DOM nodes which reduces the heap size occupied by the references of these elements. It maintains the frame rate while scrolling in case of longer lists. Let’s take a look at how this is achieved. List Virtualization needs the data that is to be shown as a list, let’s say n in the length. Then we decide how many list items are to be shown at a time, let’s say t. Based on this we will control the range of items that is being currently shown to the user. As the user scrolls down, we increment the window in a way that it starts showing the next set of items. So, if it starts with index [0, t], upon scrolling it will change to [1, t+1] and so on. To decide when this increment should happen, we use the scroll height of the container element. Let’s go further in detail. Each item is given a fixed height in this list and their position is set relative to the container based on their index and height. If the fixed height of each item is 10px, then the first element at 0, second at 10px, third at 20px…As the user scrolls, dividing the scrollTop with the default item height, we get how many items down has the user scrolled. This value is where our window starts and adding t in this value gives us the last element of our window. Together, this dynamic change of window range and the respective position of each item in this window gives the illusion that the user is scrolling through a static list of items. To understand this whole workflow better, break it intentionally or introduce some bugs. First, break the part which is updating the range of the list to control the visible elements. Then, break the part where we are setting the positions of the elements based on their index. Then, you will understand the significance of each logical component involved.
read more