jQuery child

JavaScript performance comparison

Test case created by Todd

Info

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 code

<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>

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
$('#parent').children()
var c = $('#parent').children()[0];
pending…
$('#parent').children('.child')
var c = $('#parent').children('.child')[0];
pending…
$('.child:first-child')
var c = $('.child:first-child')[0];
pending…
$('#parent .child:first-child')
var c = $('#parent .child:first-child')[0];
pending…
$('#parent :first-child')
var c = $('#parent :first-child')[0];
pending…
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')
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