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

JavaScript performance comparison

Test case created

Preparation code

<script src="//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

Test runner

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

Java applet disabled.

Testing in unknown unknown
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

Add a comment