getComputedStyle vs style vs .css() (v17)

Revision 17 of this benchmark created on


Description

getComputedStyle vs style vs .css()

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="element" style="width:200px">My Element</div>
<script>
  var element = document.getElementById("element");
  var $element = $("#element");
  var getStyle = (function(){
      return (document.documentElement.currentStyle ?
        function(obj){
          return obj.currentStyle
        }
      :
        function(obj, style){
          return window.getComputedStyle(obj, null)[style]
        }
      );
  }());

</script>

Setup

var wnd = Object.create(window);
    var _window = window;
    var _document = _window.document;
    var _count = 1000;

Test runner

Ready to run.

Testing in
TestOps/sec
defaultView-getComputedStyle
for (var i = 0; i < _count; i++) {
  var width0 = _document.defaultView.getComputedStyle(element, null).getPropertyValue("width");
}
ready
defaultView-getComputedStyle
for (var i = 0; i < _count; i++) {
  var width1 = _document.defaultView.getComputedStyle(element, null).width;
}
ready
.css()
for (var i = 0; i < _count; i++) {
  var width3 = $element.css("width");
}
ready
window-getComputedStyle
for (var i = 0; i < _count; i++) {
  var width4 = _window.getComputedStyle(element, null).width;
}
ready
helper function
for (var i = 0; i < 1000; i++) {
  _count width5 = getStyle(element, 'width');
}
ready

Revisions

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