$().parents() vs. $().is()

JavaScript performance comparison

Test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class="test1">
<div class="test2">
<div class="test3">
</div>
</div>
<div class="test2">
</div>
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var testNo = 1000 + Math.floor(Math.random() * 9000),
    test1 = $('.test1'),
    test3 = $('.test3'),
    className = 'test-' + testNo;
  
  test1.attr('class', 'test1 ' + className);

};
</script>

Preparation code output

<div class="test1"> <div class="test2"> <div class="test3"> </div> </div> <div class="test2"> </div> </div>

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
$().parents()
var hasParent = test3.parents('.' + className + ':first').length > 0;
pending…
$().is()
var hasParent = test3.is('.' + className + ' *');
pending…
$().is() with concrete class
var hasParent = test3.is('.' + className + ' .test3');
pending…

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

Compare results of other browsers

0 Comments