getComputedStyle vs style vs .css()

JavaScript performance comparison

Revision 8 of this test case created

Info

getComputedStyle vs style vs .css()

Preparation code

<script src="//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>

Preparation code output

My Element

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
window-getComputedStyle
var width1 = parseInt(window.getComputedStyle(element, null).getPropertyValue('width'), 10);
pending…
style
var width2 = parseInt(element.style.width, 10);
pending…
.css()
var width3 = $element.css("width");
pending…
window-getComputedStyleWarp
var width1 = parseInt(window.getComputedStyleWarp(element, null).getPropertyValue('width'), 10);
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