jQuery vs. document.querySelector

JavaScript performance comparison

Test case created by Lee Brimelow

Preparation code

<div id="main">
  <ul class="gallery">
    <li>
      Hello World
    </li>
    <li>
      Hello World
    </li>
    <li>
      Hello World
    </li>
    <li>
      Hello World
    </li>
    <li>
      Hello World
    </li>
    <li>
      Hello World
    </li>
  </ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.teardown = function() {
  var el = document.querySelector("#main ul:first-child");
  el.style.display = "list-item";

};
</script>

Preparation code output

<div id="main"> <ul class="gallery"> <li> Hello World </li> <li> Hello World </li> <li> Hello World </li> <li> Hello World </li> <li> Hello World </li> <li> Hello World </li> </ul> </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
jQuery
$("#main ul:first-child").hide();
pending…
DOM
var el = document.querySelector("#main ul:first-child");
el.style.display = "none";
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.

3 Comments

Samuel Ondrek commented :

Nice! This test exactly I'm trying to find :) Thanks Lee.

Samuel Ondrek commented :

I used yours sample, in my blog. FYI.

olvlvl commented :

Hi Lee,

Isn't the call to the hide() method impacting performances ? The following code might be fairer:

var els = $("#main ul:first-child");
els[0].style.display = "none";

Or better, don't change the display.