jQuery is() vs regexp for css visibility

JavaScript performance comparison

Test case created by Max Leske

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<p id="one" style="display:none">blah</p>
<p id="two" style="visibility:hidden"
      
<script>
Benchmark.prototype.setup = function() {
  var isVisible = function(element) {
  		var element = $(element);
  		return !(/none/i.test(element.css('display'))) 
  			&& !(/hidden/i.test(element.css('visibility')));
  }

};
</script>

Preparation code output

<p id="one" style="display:none">blah</p> <p id="two" style="visibility:hidden"

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
use is() to check if hidden
$("#two").is(":hidden");
pending…
use regexp to check if hidden
/hidden/i.test($("#two").css('visibility'));
pending…
use is() to check if visible
$("#one").is(":visible");
pending…
use regexp to check if visible
isVisible($("#one"));
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments