style versus jquery css

JavaScript performance comparison

Revision 24 of this test case created

Preparation code

<div id="test" style="width: 400px; height: 200px; background-color: #ccc;">
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    function css(elements, obj) {
      for (var e = 0, elen = elements.length; e < elen; e++) {
        for (var i in obj) {
          elements[e].style[i] = obj[i]
        }
      }
    }
   
    function css2(elements, obj) {
      var style = elements.style;
      if (!style) {
        for (var e = 0, elen = elements.length; e < elen; e++) {
          css2(elements[e], obj)
        }
      } else {
        for (var i in obj) {
          style[i] = obj[i]
        }
      }
    }
   
    var $test = $('#test')[0];
    var $testq = $('#test');
    var cachedElement = document.getElementById('test');
    var cachedStyle = document.getElementById('test').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 css()
$('#test').css('background-color', '#000000');
pending…
Raw style
document.getElementById('test').style.backgroundColor = "#000000";
pending…
Custom css() function
css([document.getElementById('test')], {
  backgroundColor: "#000000"
});
pending…
Custom css2() function
css2(document.getElementById('test'), {
  backgroundColor: "#000000"
});
pending…
Custom css2() function, array
css2([document.getElementById('test')], {
  backgroundColor: "#000000"
});
pending…
cachedElement
cachedElement.style.backgroundColor = "#000000";
pending…
cachedStyle
cachedStyle.backgroundColor = "#000000";
pending…
cachedjQuery + style
$test.style.backgroundColor = '#000000';
pending…
cachedjQuery + css()
$testq.css('background-color', '#000000');
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