style versus jquery css (v35)

Revision 35 of this benchmark created on


Preparation HTML

<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>

Setup

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;

Test runner

Ready to run.

Testing in
TestOps/sec
Jquery css()
$('#test').css('background-color', '#000000');
ready
Raw style
document.getElementById('test').style.backgroundColor = "#000000";
ready
Custom css() function
css([document.getElementById('test')], {
  backgroundColor: "#000000"
});
ready
Custom css2() function
css2(document.getElementById('test'), {
  backgroundColor: "#000000"
});
ready
Custom css2() function, array
css2([document.getElementById('test')], {
  backgroundColor: "#000000"
});
ready
cachedElement.style.backgroundColor = "#000000";
ready
cachedStyle.backgroundColor = "#000000";
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.