jquery visible test (v12)

Revision 12 of this benchmark created on


Description

http://stackoverflow.com/questions/4696779/performance-of-jquery-visible

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="test-content"></div>
<script>
  $(function() {
   for (var i = 200; i--;) {
    $('<span />', {
     style: "display:" + ((i % 2) ? 'block' : 'none'),
     class: "test-span" + ((i % 2) ? ' visi' : ' notvisi'),
     text: i
    }).appendTo('#test-content');
   }
  });
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
:visible
var spans = $('.test-span'), foo = 0;
spans.each(function(index) {
    if ($(this).is(':visible')) {
        foo++;
    }
});
console.log(foo);
ready
normal
var spans = $('.test-span'), foo = 0;
for (var i = 0, len = spans.length; i<len; i++) {
    if (spans[i].offsetParent) {
        foo++;
    }
}
console.log(foo);
ready

Revisions

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