Speed difference between inserting html and changing display style property

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Speed difference between inserting html and changing display style property



Assuming you have a relatively small piece of HTML (let's say under 100 tags and <4KB in size) and you want to occasionally display and hide it from your user (think menu, modal... etc).



Is the fastest approach to hide and show it using css, such as:


//Hide:
document.getElementById('my_element').style.display = 'none';

//Show:
document.getElementById('my_element').style.display = 'block';



Or to insert and remove it:


//Hide
document.getElementById('my_element_container').innerHTML = '';

//Show:
const my_element_html = contents of the element;
document.getElementById('my_element_container').innerHTML = my_element_html;

// Note: insertAdjacentHTML is obviously faster when the container has other elements, but I'm showcasing this using innerHTML to get my point across, not necessarily because it's always the most efficient of the two.



Obviously, this can be benchmarked on a case by case basis, but, with some many browser versions and devices out there, any benchmarks that I'd be able to run in a reasonable amount of time aren't that meaningful.



I've been unable to find any benchmarks related to this subject.



Are there any up to date benchmarks comparing the two approaches? Is there any consensus from browsers developers as to which should, generally speaking, be preferred when it comes to speed.





What does JSPerf say - my gut feeling is with any significant HTML content, there will be an order of magnitude difference between show/hide and remove and insert
– mplungjan
Aug 12 at 12:13






Both, changing the display and remving the innerHTML will cause a reflow, those are bad for performance, can you use visibility to hide the element? That would be easier on the browser.
– Luca Kiebel
Aug 12 at 12:13





I would go with the first one because you dont have to rebuild the content every time.
– Jonas Wilms
Aug 12 at 12:16





@Luca In my experience the behavior of elements when changing the visibility property is very unreliable and hard to understand and design properly around. So the extra loading speed there never seemed worth it considering the mental overhead.
– George
Aug 12 at 12:24


visibility





@Luca, also, considering that they both cause a reflow, would that essentially mean they are similar in performance ?
– George
Aug 12 at 12:25




1 Answer
1



In principle, DOM manipulation is slower than toggling display property of existing nodes. And I could stop my answer here, as this is technically correct.


display



However, repaint and reflow of the page is typically way slower and both your methods trigger it so you could be looking at:



Which leaves you comparing 101 units with 102 units, instead of comparing 3 with 4 (or 6 with 7). I'm not saying that's the order of magnitude, it really depends on the actual DOM tree of your real page, but chances are it's close to the figures above.



If you use methods like: visibility:hidden or opacity:0, it will be way faster, not to mention opacity is animatable, which, in modern UIs, is preferred.


visibility:hidden


opacity:0


opacity



A few resources:



Web performance, much like web development, is not a "press this button" process. You need to try, fail, learn, try again, fail again...



If your elements are always the same, you might find out (upon testing) caching them inside a variable is much faster than recreating them when your show method is called.



Testing is quite simple:


performance.now()


n


n



Compare. You will notice conclusions drawn from 100 test are quite different than the ones from 1e7 test.



To go even deeper, you can test differences for different methods when showing and for different methods when hiding. You could test rendering elements hidden and toggle their display afterwards. Get creative. Try anything you can think of, even if it seems silly or doesn't make much sense.



That's how you learn.





So, tl;dr: It depends, so test it. Which is the answer to pretty much every performance question on SO :).
– Heretic Monkey
Aug 12 at 16:08





@HereticMonkey, pretty much. I'd take out the "on SO" part, though.
– Andrei Gheorghiu
Aug 12 at 16:10






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard