Is it worth checking the visibility of a DOM element before switching its visibility? Or is it premature optimization?

Check out the following jQuery code:

if ($(this).is(':hidden')) {
    $(this).show();
}

My question is:

  • Is it worth checking the visibility of an element before issuing the show () command?
    • i.e. Are DOMs more expensive than DOMs and does this template include a small performance optimization?
  • Or a visibility check without any utility, and will it be pure code to simply issue the show () command unconditionally?
+5
source share
3 answers

, , , - . , , , , .

, 25% . ( ) http://jsperf.com/is-hidden-check.

+6

, , , , :

:

: jQuery 1.3.2. , , . CSS ( $(elem).css('visibility','hidden').is(':hidden') == false).

, , . :hidden , , , 5 , , , .

show(); , , , jQuery, -, show/: D

+2

your visibility check may save some effort because it will not produce .show () if it is not hidden.

So i think let's go with him

0
source

All Articles