jQuery child (v13)

Revision 13 of this benchmark created on


Description

What's the fastest way to get a child in jQuery? .children(), .next() or :first-child? I know the class-name in advance.

http://www.quora.com/Whats-the-fastest-way-to-get-a-child-in-jQuery

Preparation HTML

<div id="parent" class="parent">
  <div id="child" class="child">
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script>
  var p = $('#parent');
</script>

Setup

p.children(':first');

Test runner

Ready to run.

Testing in
TestOps/sec
$('#parent').children()
var c = $('#parent').children(':first');
ready
$('#parent').children('.child')
var c = $('#parent').children()[0];
ready
$('.child:first-child')
var c = $('.child:first-child')[0];
ready
$('#parent .child:first-child')
var c = $('#parent .child:first-child')[0];
ready
$('#parent :first-child')
var c = $('#parent :first-child')[0];
ready
p.children()
// When you already have an object p = $('#parent')
var c = p.children()[0];

// If there's a huge speed improvement here, try the other variations with p instead of $('#parent')
ready
$('#parent').find(":first")
var c = $('#parent').find(":first");
ready
$('#parent').find(":nth-child(1)")
var c = $('#parent').find(":nth-child(1)");
ready

Revisions

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