First element matching

JavaScript performance comparison

Test case created by Robert Koritnik

Info

Compares different techniques to match first matching element.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="first"><span class="test"></span></div>
<div id="second"><span class="test"></span><div>
<div id="e3"><span class="test"></span><div>
<div id="e4"><span class="test"></span><div>
<div id="e5"><span class="test"></span><div>
<div id="e6"><span class="test"></span><div>
<div id="e7"><span class="test"></span><div>
<div id="e8"><span class="test"></span><div>
<div id="e9"><span class="test"></span><div>
<div id="e10"><span class="test"></span><div>

Preparation code output

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
$("span.test:first").text("Welcome");
pending…
#first
$("#first span.test").text("Welcome");
pending…
.first()
$("span.test").first().text("Welcome");
pending…
eq(0)
$("span.test").eq(0).text("Welcome");
pending…
#first as context
$("span.test", "#first").text("Welcome");
pending…

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

Compare results of other browsers

1 comment

commented :

Add a comment