15
I really like using libraries like Vue
and AngularJs
and my favorite implementation was to Infinite Scroll (or loading on demand). I mean, I initially upload 15 records via ajax. If the user scrolls the page, loads 15 more, and so on until the records are finished, and assembling the elements in the DOM accordingly.
I keep wondering if this operation can cause any performance problems regarding memory or even navigation (causing crashes, for example).
I don’t want to get into discussions about the Angular Watcher, ng-repeat and the like, but specifically about the DOM.
Having many elements in the DOM could harm performance?
For example, if I do an Infinite scroll on my page, and it generates about 10,000 Ivs, it could impact the performance of my page?
Is there any recommendation on this?
Example:
var listElm = document.querySelector('#infinite-list');
var nextItem = 1;
var loadMore = function() {
for (var i = 0; i < 100; i++) {
var item = document.createElement('li');
item.innerText = 'Item ' + nextItem++;
listElm.appendChild(item);
}
}
listElm.addEventListener('scroll', function() {
if (listElm.scrollTop + listElm.clientHeight >= listElm.scrollHeight) {
loadMore();
}
});
loadMore();
#infinite-list {
/* We need to limit the height and show a scrollbar */
width: 200px;
height: 300px;
overflow: auto;
/* Optional, only to check that it works with margin/padding */
margin: 30px;
padding: 20px;
border: 10px solid black;
}
/* Optional eye candy below: */
li {
padding: 10px;
list-style-type: none;
}
li:hover {
background: #ccc;
}
<ul id='infinite-list'>
</ul>
Yes, it is that I have no time to answer explaining how the rendering process occurs, but it can consume a lot, however there are strategies to avoid this, such as temporarily detaching the elements, of course it is a relative gain and it is something complicated to apply, even more that by injecting certain events again can be triggered, of course this can be solved. There is also the question of GC and the element even being detached may still be referenced in some variable/object, which is usually many frameworks problem.
– Guilherme Nascimento
A process that avoids unnecessary use of processing/memory in the case of very large lists is called Virtual List.
– Kazzkiq