change css in many ways

JavaScript performance comparison

Revision 4 of this test case created by Marcin

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var left = 10,
        top = 10,
        el = document.createElement('DIV'),
        styl;
   
    this.elStyle = el.style;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
style.x
el.style.left = left + "px";
el.style.top  = top  + "px";
pending…
cached style.x
styl = el.style;

styl.left = left + "px";
styl.top = top + "px";
pending…
cssText
el.style.cssText += "; left: " + left + "px; top: " + top + "px;";
pending…
$.css
$(el).css({
  left: left,
  top: top
});
pending…
setAttribute
el.setAttribute("style", "left: " + left + "px; top: " + top + "px;");
pending…
this.elStyle
this.elStyle.left = left + "px";
this.elStyle.top = top + "px";
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