local-variable-vs-object-property

JavaScript performance comparison

Revision 2 of this 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,
    boundScopeObjectProperty;
  
  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');
  }
  boundScopeObjectProperty = function boundScopeObjectProperty() {
    this.a.setAttribute('class', 'b');
    this.a.removeAttribute('class');
  }.bind(obj);

};
</script>

Preparation code output

<div id="a"></div> <div id="b" class="omega"></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
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…
boundScope
count = 10000;
while (count--) boundScopeObjectProperty();
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.

0 Comments