$("#id") vs $(document.getElementById)

JavaScript performance comparison

Revision 16 of this test case created by altryne

Preparation code

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

<div id="LOL"></div>
<script>
  var $el = $(0);
</script>

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
#id
$("#LOL");
pending…
native
$(document.getElementById("LOL"));
pending…
Sizzle
$("[id=LOL]");
pending…
common mistake
$('div#LOL');
pending…
native queryselectorall
$(document.querySelectorAll('#LOL'))
pending…
native queryselector
$(document.querySelector('#LOL'))
pending…
manual jquery collection
$el.context = $el[0] = document.getElementById("LOL")
// $el == $(document.getElementById("LOL"))
pending…
native chrome global var from ID
if(LOL){
$el.context = LOL
}
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:

0 comments

Add a comment