local-variable-vs-object-property

JavaScript performance comparison

Test case created by Tomasz

Preparation code

<div id="a"></div>
<div id="b" class="omega"></div>
<script>
Benchmark.prototype.setup = function() {
    var obj = {
      a: document.getElementById('a'),
      b: 1000,
      c: 1001,
      d: 2020,
      e: 'sshjhjkhkhjkhjk',
      scopeProperty: function scopeProperty() {
         this.a.setAttribute('class', 'b');
         this.a.removeAttribute('class');
      }
    },
      count,
      bindScopebjectProperty;
   
    function localVariable(o) {
      var element = o.a;
      element.setAttribute('class', 'b');
      element.removeAttribute('class');
    }
   
    function objectProperty(o) {
      o.a.setAttribute('class', 'b');
      o.a.removeAttribute('class');
    }
    function globalObjectProperty() {
      obj.a.setAttribute('class', 'b');
      obj.a.removeAttribute('class');
    }
    bindScopeObjectProperty = function bindScopeObjectProperty() {
      this.a.setAttribute('class', 'b');
      this.a.removeAttribute('class');
    }.bind(obj);
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
localVariable
count = 10000;
while (count--) localVariable(obj);
pending…
objectProperty
count = 10000;
while (count--) objectProperty(obj);
pending…
scopeProperty
count = 10000;
while (count--) obj.scopeProperty()
pending…
globalObjectProperty
count = 10000;
while (count--) globalObjectProperty();
pending…
bindScope
count = 10000;
while (count--) bindScopeObjectProperty();
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