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

Revision 25 of this benchmark created on


Description

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

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

Preparation HTML

<div id="root" style="display:none"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
 var content = '';
for (var j = 0; i < 1000; j++) {
 content += '<ul>';
 for (var i = 0; i < 100; i++) {
  content += '<li>' + i + '</li>';
 }
 content += '</ul>';
}
 $('#root').append(content);
</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

Revisions

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