Vanilla JS FTW (jQuery vs. JS)

JavaScript performance comparison

Test case created by srigi

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div>
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>
<div id="wrap">
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
  <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
  <div>
      <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
  </div>
</div>
<div>
  <img id="about" src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png">
</div>

      
<script>
Benchmark.prototype.setup = function() {
  newDiv = document.createElement("div");
  $newDiv = $("<div/>");

};

Benchmark.prototype.teardown = function() {
  var divs,
      newDiv,
      $newDiv,
      parent,
      cloned,
      nextElement;

};
</script>

Preparation code output

<div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> <div id="wrap"> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> <div> <img src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </div> </div> <div> <img id="about" src="http://www.zssmt.sk/e107/e107_images/icons/icon20.png"> </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
fetch elements by tag (jQuery)
divs = $("div");
pending…
fetch elements by tag (raw JS)
divs = document.querySelectorAll("div");
pending…
create element (jQuery ..)
newDiv = $("<div/>");
pending…
create element (raw JS ++)
newDiv = document.createElement("div");
pending…
Add class ..
$newDiv.addClass("foo");
pending…
Add class ++
newDiv.classList.add("foo");
pending…
Append new element ..
$("body").append("<p/>");
pending…
Append new element ++
document.body.appendChild(document.createElement("p"));
pending…
Filter elements, add attribute ..
$("img").filter(":first").attr("alt", "Hi!");
pending…
Filter elements, add attribute ++
document.querySelector("img").setAttribute("alt", "Hi!");
pending…
Parent of ID node ..
parent = $("#about").parent();
pending…
Parent of ID node ++
parent = document.getElementById("about").parentNode;
pending…
Clone ID node ..
cloned = $("#about").clone();
pending…
Clone ID node ++
cloned = document.getElementById("about").cloneNode(true);
pending…
Empty ID node ..
$("#wrap").empty();
pending…
Empty ID node ++
var wrap = document.getElementById("wrap");
while (wrap.firstChild) {
  wrap.removeChild(wrap.firstChild);
}
pending…
Find next() of ID node ..
nextElement = $("#wrap").next();
pending…
Find next() of ID node ++
nextElement = document.getElementById("wrap").nextSibling;
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