Performance with infinite scrolling or multiple dom elements?

I have a question about a lot of items and performance.

Let's say I have 6,000 dom elements per page, and the number of elements can be increased as the user interacts with the page (the user scrolls to create a new dom element), like twitter.

To improve page performance, I can only think of two things.

  • set display to invisible elements to anyone to avoid rearrangement
  • remove invisible elements from dom, then re-add them as needed.

Are there other ways to improve a page with a lot of dom elements?

+66
performance javascript dom
Sep 27 '12 at 2:11
source share
3 answers

There is no experience with this, but there are some useful tips here: http://engineering.linkedin.com/linkedin-ipad-5-techniques-smooth-infinite-scrolling-html5

I looked at Facebook and they seem to be doing nothing, particularly in Firefox. When scrolling down, the DOM elements at the top of the page do not change. Firefoxโ€™s memory usage is increased to 500 megabytes before Facebook lets you scroll further.

Twitter looks just like Facebook.

Google Maps is another story from which maps come out of sight, are removed from the DOM (although not immediately).

+24
Sep 27 '12 at 2:28
source share

We had to face a similar problem on FoldingText . As the document grew larger, more line elements and related span elements were created. The browser mechanism was simply suffocating, and therefore it was necessary to find the best solution.

Here's what we have done, whether or not it can be useful for your purposes:

Render the entire page as a long document and browser window as a lens for a specific part of a long document. You really need to show only part of the lens.

So, the first part is designed to calculate the visible port. (It depends on how your elements are placed, absolute / fixed / default)

var top = document.scrollTop; var width = window.innerWidth; var height = window.innerHeight; 

A few more resources to find a more viewport based on multiple browsers:

Get browser viewport sizes using JavaScript

Cross browser scrollTop detection method of browser window

Secondly, you need a data structure to know which elements are visible in this area.

We already have a balanced binary search tree for editing text, so we also expanded it to control line heights, so this part was relatively simple for us. I do not think that you will need a complex data structure to control the heights of the elements; a simple array or object may work fine. Just make sure you can easily request heights and sizes. Now, how would you get height data for all of your elements. Very simple (but computationally expensive for lots of items!)

 var boundingRect = element.getBoundingClientRect() 

I speak in terms of pure javascript, but if you use jQuery $.offset , $.position and the methods listed here would be very helpful.

Again, using a data structure is only important as a cache, but if you want, you can do it on the fly (although, as I said, these operations are expensive). Also, beware of changing CSS styles and calling these methods. These functions force redraw, so you will see a performance problem.

Finally, just replace the elements on the screen with a single, like a <div> with a calculated height

  • Now that you have the heights for all the elements stored in your data structure, request all the elements that lie before the visible viewport.

  • Create a <div> with a given css height (in pixels) to the sum of the element heights

  • Mark it with the class name so you know its div filler
  • Remove all elements from dom so this div closes
  • insert this newly created div instead

Repeat for elements that lie after the visible viewport.

Look for scroll and resize events. In each scroll, you will need to return to your data structure, remove the placeholders, create items that were previously removed from the screen, and add new placeholders.

:) This is a long, complicated method, but for large documents it increased our productivity by a large margin.

TL; DR

I'm not sure I explained it correctly, but the gist of this method is:

  • Know the vertical dimensions of your elements.
  • Know the scroll view port
  • Represent all elements off-screen with a single div (height equal to the sum of all the heights of the elements that it covers)
  • At any given time, you will need two divs, one for the elements above the visible viewport, one for the elements below.
  • Keep an eye on the viewing port while listening to scroll and resize events. Re-create divs and visible elements.

Hope this helps.

+95
Sep 27
source share

This is 2019. The question is really old, but I think it is still relevant and interesting, and maybe something has changed today, as we are all also inclined to use React JS now.

I noticed that the Facebook timeline seems to use clusters of content that is hidden with display: none !important as soon as the cluster goes out of view, so all previously rendered DOM elements are saved in the DOM, just those views are hidden with display: none !important . In addition, the total height of the hidden cluster is set by the div parent div hidden cluster.

Here are some screenshots I took:

enter image description here

enter image description here

enter image description here

What do you think of this approach in 2019? Also, for those using React, how can this be implemented in React? It would be great to get your opinions and thoughts on this complex topic.

Thanks for your attention!

0
Jul 10 '19 at 22:10
source share



All Articles