style versus jquery css (v21)

Revision 21 of this benchmark created on


Preparation HTML

<div id="test" style="width: 400px; height: 200px; background-color: #ccc;">
</div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></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
cachedElement.style.backgroundColor = "#000000";
ready
cachedStyle
cachedStyle.backgroundColor = "#000000";
ready
d3 style
d3.select('#test').style('background-color', '#000000');
ready

Revisions

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