Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance implications when hiding a DIV by rendering it offscreen

What are the performance implications of hiding a complex portion of an HTML document within an offscreen DIV like:

<div style="position:absolute;top:-10000px;left:-10000px;">
  Lots of HTML here...
</div>

as compared to using "display: none" or "visiblity: hidden" ?

Is there a performance/memory-usage penalty? How bad is it? Can this be advisable if the targets are mobile browsers (iPhone/Android)?

like image 309
Sergio Avatar asked Aug 10 '11 16:08

Sergio


People also ask

Does display none improve performance?

Display none don't improve the performance because the goal of virtual scrolling is reduce the number of the elements into the dom. Make an element display none or remove an element, trigger a reflow, but with display none you worst the performance because you don't have the benefit of reduce the dom.

What is offscreen rendering?

Offscreen rendering lets you obtain the content of a BrowserWindow in a bitmap, so it can be rendered anywhere, for example, on texture in a 3D scene. The offscreen rendering in Electron uses a similar approach to that of the Chromium Embedded Framework project.

Does display none render?

display: none : hides the element and destroys its rendering state. This means unhiding the element is as expensive as rendering a new element with the same contents. visibility: hidden : hides the element and keeps its rendering state.


2 Answers

Performance-wise you're best to build up your HTML as a string as insert it into the DOM in one go. DOM changes trigger redraws, so the fewer DOM changes the better.

like image 42
Diodeus - James MacFarlane Avatar answered Nov 16 '22 01:11

Diodeus - James MacFarlane


It depends on the browser. Here is good article about rendering, reflow repaint in browsers. So theoretically it should be not rendered when something changed on the side as all absolute positioned elements will not rerendered when something changed in there parent elements. So it should perform better then display:none which will be rerendered in IE for example. But you have still a lot of DOM elements in browser memory. So maybe its better to put the elements out of the DOM and add them again later.

like image 117
Andreas Köberle Avatar answered Nov 16 '22 02:11

Andreas Köberle