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

JavaScript performance comparison

Test case created

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

Test runner

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

Java applet disabled.

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


Comment form temporarily disabled.

Add a comment