$().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 class="test2">
Benchmark.prototype.setup = function() {
  var testNo = 1000 + Math.floor(Math.random() * 9000),
    test1 = $('.test1'),
    test3 = $('.test3'),
    className = 'test-' + testNo;
  test1.attr('class', 'test1 ' + className);


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

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

Compare results of other browsers