JQuery :first vs .first() vs. eq

JavaScript performance comparison

Revision 10 of this test case created by Marcin

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="test">
  <strong>
    test
  </strong>
  <span>
    aaaa
  </span>
</div>
<script>
  var $div = $("#test");

  function test(el) {
    if (el.nodeName !== 'SPAN') {
      throw new Error('No span');
    }
  }
</script>
    

Preparation code output

<div id="test"> <strong> test </strong> <span> aaaa </span> </div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
.first
var res = $div.find('span').first()[0];
test(res);
pending…
:first
var res = $div.find('span:first')[0];
test(res);
pending…
getElementsByTagName
var res = $div[0].getElementsByTagName('span')[0];
test(res);
pending…
[0]
var res = $div.find('span')[0];
test(res);
pending…
$(getElementsByTagName)
var res = $div[0].getElementsByTagName('span')[0];
test(res);
var $res = $(res);
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.

0 Comments

test aaaa