Skip to content

Instantly share code, notes, and snippets.

@edvinasbartkus
Created January 12, 2012 11:00
Show Gist options
  • Save edvinasbartkus/1599884 to your computer and use it in GitHub Desktop.
Save edvinasbartkus/1599884 to your computer and use it in GitHub Desktop.
Vanilla JS vs jQuery
/*
There is this file shared about the performance of vanilla JS and jQuery performance
http://sharedfil.es/j-3k2NPAjwfQ.html
We are geeks and we want real numbers
Test suite for www.jquery.com
It is dummy test suite
Chromium 18.0.997.0 (Developer Build 116462 Linux) Built on Ubuntu 11.10, running on LinuxMint 12
*/
window.prev_result = null;
var test = function (f) {
var a,b;
a = new Date().getTime();
for(var i=0; i<1000; i++) {
f();
};
b = new Date().getTime();
var result = b-a;
if(window.prev_result) {
var diff = result / window.prev_result * 100;
console.log(window.prev_result + " " + result + " " + diff);
window.prev_result = null;
} else {
window.prev_result = result;
}
};
// JQUERY milliseconds / RAW milliseconds / %
test(function() { var divs = $("div"); });
test(function() { var divs = document.querySelectorAll("div"); });
// 31 18 58.06451612903226
test(function() { var newDiv = $("<div />"); });
test(function() { var newDiv = document.createElement("div"); });
// 12 3 25
window.newDiv = $("<div />");
test(function() { window.newDiv.addClass("foo"); });
window.newDiv = document.createElement("div");
test(function() { window.newDiv.classList.add("foo"); });
// 18 3 16.666666666666664
test(function() { $("body").append("<p/>"); });
test(function() { document.body.appendChild(document.createElement("p")); });
// 27 5 18.51851851851852
test(function() { $("img").filter(":first").attr("alt", "Hi!"); });
test(function() { document.querySelector("img").setAttribute("alt", "Hi!"); });
// 78 7 8.974358974358974
test(function() { var parent = $("#jq-header").parent(); });
test(function() { var parent = document.getElementById("jq-header").parentNode; });
// 21 1 4.761904761904762
test(function() { var cloned = $("#jq-header").clone(); });
test(function() { var cloned = document.getElementById("jq-header").cloneNode(true); });
// 215 193 89.76744186046511
test(function() { $("#jq-header").empty(); });
test(function() { var wrap = document.getElementById("jq-header");
while(wrap.firstChild) wrap.removeChild(wrap.firstChild); });
// 7 1 14.285714285714285
test(function() { var nextElement = $("#jq-header").next(); });
test(function() { var nextElement = document.getElementById("jq-header").nextSibling; });
// 20 1 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment