style versus jquery css - get

JavaScript performance comparison

Revision 22 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 cachedElement = document.getElementById('test');
    var cachedStyle = document.getElementById('test').style;
    var cachedJqueryObject = $('#test');
    var shadowObject = {'background-color': '#ccc'};
};
</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()
var color = $('#test').css('background-color');
pending…
Raw style
var color = document.getElementById('test').style.backgroundColor;
pending…
cachedElement
var color = cachedElement.style.backgroundColor;
pending…
cachedStyle
var color = cachedStyle.backgroundColor;
pending…
Cached Jquery object css()
var color = cachedJqueryObject.css('background-color');
pending…
Shadow object
var color = shadowObject['background-color'];
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