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

Revision 11 of this benchmark created by usmonster on


Description

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

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

Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
.first()
console.log(
  $('ul li').first().text()
);
ready
:first
console.log(
  $('ul li:first').text()
);
ready
.eq(0)
console.log(
  $('ul li').eq(0).text()
);
ready
.filter(":first")
console.log(
  $('ul li').filter(":first").text()
);
ready

Revisions

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