.first() vs :first vs eq(0)

JavaScript performance comparison

Revision 17 of this test case created

Info

which is faster .first(), :first or .eq(0)?

Stack Overflow Question: http://stackoverflow.com/questions/2312761/jquery-first-vs-first

Slightly more complicated DOM.

Preparation code

<div><div>
<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
</ul>
</div></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 

Preparation code output

  • One
  • Two
  • Three
  • Four
  • Five
  • One
  • Two
  • Three
  • Four
  • Five
  • One
  • Two
  • Three
  • Four
  • Five
  • One
  • Two
  • Three
  • Four
  • Five

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
.first()
$('ul li').first().length == 0
 
pending…
:first
$('ul li:first').length == 0
 
pending…
.eq(0)
$('ul li').eq(0).length == 0
 
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