jQuery .css() vs. MooTools .setStyles() vs. DOM .style

JavaScript performance comparison

Test case created by Yu-Cheng Chuang

Info

Testing for jQuery .css(), MooTools .setStyles() and native DOM element.style performance, as well as element.style.cssText.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.3/mootools-yui-compressed.js"></script>
<div id="test">Hello! World</div>
 
<script>
Benchmark.prototype.setup = function() {
    var element = document.getElementById("test");
    var $element = $(element); // avoid overhead on jQuery object wrapping
   
};

Benchmark.prototype.teardown = function() {
    document.getElementById("test").removeAttribute("style");
};
</script>

Preparation code output

Hello! World

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jQuery .css()
$element.css({
  "height": "320px", // eliminate `+= "px"' overhead
  "width": "240px",
  "color": "orange",
  "background-color": "black"
});
pending…
element.style
element.style.height = "320px";
element.style.width = "240px";
element.style.color = "orange";
element.style.backgroundColor = "black";
 
pending…
element.style.cssText=
element.style.cssText = "height: 320px; width: 240px; color: orange; background-color: black;";
 
pending…
MooTools .setStyles()
element.setStyles({
  "height": "320px",
  "width": "240px",
  "color": "orange",
  "background-color": "black"
});
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