Selecting the first

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<div>
        <ul>
        <li class="answer">Foo</li>
        <li class="answer">Bar</li>
        <li class="answer">Moo</li>
        <li class="answer">Cat</li>
        <ul>
</div>
 

Preparation code output

  • Foo
  • Bar
  • Moo
  • Cat

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() method
$('.answer').first();
pending…
:first sizzle
$('.answer:first');
pending…
.eq(0) method
$('.answer').eq(0);
pending…
:eq(0) sizzle
$('.answer:eq(0)');
pending…
$($(".answer")[0]) nested jQuery object
$($(".answer")[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:

2 comments

Jeroen Ransijn commented :

Nice benchmarks!

I want to know if a class exists, returning a boolean, would be the fastest way to achieve this using…

!($($('.selector')[0]).length === 0);

or

!($('.selector').length === 0);

Maybe you can include $('.selector') tests?

Thanks!

Jeroen

Add a comment