getComputedStyle vs style vs .css() (v8)

Revision 8 of this benchmark created on


Description

getComputedStyle vs style vs .css()

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<div id="element" style="width:200px">
  My Element
</div>
<script>
  var element = document.getElementById("element");
  var $element = $("#element");

  if (!window.getComputedStyle) {
    window.getComputedStyle = function(el, pseudo) {
      this.el = el;
      this.getPropertyValue = function(prop) {
        var re = /(\-([a-z]){1})/g;
        if (prop == 'float') prop = 'styleFloat';
        if (re.test(prop)) {
          prop = prop.replace(re, function() {
            return arguments[2].toUpperCase();
          });
        }
        return el.currentStyle[prop] ? el.currentStyle[prop] : null;
      }
      return this;
    }
  }

  if (!window.getComputedStyleWarp) {
    (function() {
      var elem;

      var styleObj = {
        getPropertyValue: function getPropertyValue(prop) {
          if (prop == 'float') prop = 'styleFloat';
          return elem.currentStyle[prop.toUpperCase()] || null;
        }
      }

      window.getComputedStyleWarp = function(el) {
        elem = el;
        return styleObj;
      }
    })();
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
window-getComputedStyle
var width1 = parseInt(window.getComputedStyle(element, null).getPropertyValue('width'), 10);
ready
style
var width2 = parseInt(element.style.width, 10);
ready
.css()
var width3 = $element.css("width");
ready
window-getComputedStyleWarp
var width1 = parseInt(window.getComputedStyleWarp(element, null).getPropertyValue('width'), 10);
ready

Revisions

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