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

JavaScript performance comparison

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

Preparation code

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

Preparation code output

  • 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()
console.log(
  $('ul li').first().text()
);
pending…
:first
console.log(
  $('ul li:first').text()
);
pending…
.eq(0)
console.log(
  $('ul li').eq(0).text()
);
pending…
console.log(
  $('ul li[0]').text()
);
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