change css in many ways

JavaScript performance comparison

Revision 5 of this test case created

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;
   
    styl = el.style;
    styl.px = 'px';
    this.elStyle = el.style;
   
    var $el = $(el);
    var $elcss = $(el).css;
    this.$elcss = $(el).css;
};
</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.left = left + "px";
styl.top = top + "px";
pending…
cssText
el.style.cssText += "; left: " + left + "px; top: " + top + "px;";
pending…
cached $el
$el.css('left', left).css('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…
$(el)
$(el).css({
  left: left,
  top: top
});
pending…
$elcss
$elcss({
  left: left,
  top: top
});
pending…
this.$elcss
this.$elcss({
  left: left,
  top: top
});
pending…
one more
this.$elcss('left', left);
this.$elcss('top', top);
pending…
with
with(this) {
  $elcss('left', left);
  $elcss('top', top);

};
pending…
with
with(styl) {
  left = left + px;
  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