Jquery .css() vs JS.style

JavaScript performance comparison

Revision 15 of this test case created

Info

Move an element

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="test" style="position:absolute;top:100px;left:200px;">
</div>
<div id="test2" style="position:absolute;top:100px;left:200px;">
</div>
<script>
Benchmark.prototype.setup = function() {
    var styles = {
      display: "block",
      position: "absolute",
      left: 50,
      top: "50%",
      zIndex: 1
    };
   
    function css(myelem, styles) {
      for (var style in styles) {
        if (!isNaN(styles[style])) styles[style] += "px";
        myelem.style[style] = styles[style];
      }
    }
};
</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
jQuery
jQuery("#test, #test2").css(styles);
pending…
JS
var myelem = document.getElementById("test");
myelem.style["display"] = "block";
myelem.style["position"] = "absolute";
myelem.style["left"] = 50 + "px";
myelem.style["top"] = "50%";
myelem.style["zIndex"] = 1;

var myelem = document.getElementById("test2");
myelem.style["display"] = "block";
myelem.style["position"] = "absolute";
myelem.style["left"] = 50 + "px";
myelem.style["top"] = "50%";
myelem.style["zIndex"] = 1;
pending…
JS foreach 1
var myelem = document.getElementById("test");
myelem.style.display = "block";
myelem.style.position = "absolute";
myelem.style.left = 50 + "px";
myelem.style.top = "50%";
myelem.style.zIndex = 1;
pending…
JS foreach 2
var myelem = document.getElementById("test");
for (var style in styles) {
  if (!isNaN(styles[style])) styles[style] += "px";
  myelem.style[style] = styles[style];
}
pending…
JS foreach 3
var myelem = document.getElementById("test");
for (var style in styles) {
  if (!isNaN(styles[style])) styles[style] += "px";
  myelem.style[style] = styles[style];
}
pending…
JS foreach function
var myelem = document.getElementById("test");
css(myelem, styles);
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