jQuery parent/child selectors

JavaScript performance comparison

Revision 73 of this test case created

Info

The different ways to select (context, pure selector, +find()) DOM elements using jQuery in parent/child scenarios.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div class="parentDivCls">
<div id="child01Div">
<span>
<ul>
<li>This is the first item <a href="#">I am an anchor</a></li>
<li>This is a parent<div>I am a child</div><div>I am a sibling</div></li>
<li>I am a sibling</li>
</ul>
</span>
<div>I'm the next and closest div.</div>
</div>
<span id="spTarget" class="spTargetCls" role="roleTarget">I'm a span<input id ="hdInput" type="hidden" />
</span>
<div id="nextDiv" >I'm the next div</div>
</div>
<div class="test">
</div>
<script>
  var list = document.getElementById('child01Div');
</script>

Preparation code output

  • This is the first item I am an anchor
  • This is a parent
    I am a child
    I am a sibling
  • I am a sibling
I'm the next and closest div.
I'm a span
I'm the next div

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
siblings
var $test = $('#child01Div').siblings('.spTargetCls');
pending…
parent
var $test = $('#hdInput').parent();
pending…
prev
var $test = $('#nextDiv').prev('span');
pending…
find
var $test = $('.parentDivCls').find('[role=roleTarget]');
pending…
selector and find
var $test = $('.parentDivCls').find('[role=roleTarget]');
pending…
parent/child selector
var $test = $('.parentDivCls').find('[role=roleTarget]');
pending…
immediate children
var $test = $('.parentDivCls').find('[role=roleTarget]');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment