prototype vs Object.create perf

JavaScript performance comparison

Test case created by Brandon Jones

Preparation code

<script>
  /*
   * "Traditional" Javascript Object
   */
  var ObjA = function() {
      this.foo = 0;
      this._baz = 0;
  }
  ObjA.prototype.bar = function(value) {
      return Math.sqrt(value);
  }
  ObjA.prototype.setBaz = function(value) {
      this._baz = value;
  }
  ObjA.prototype.getBaz = function() {
      return this._baz;
  }
  
  var instanceA = new ObjA();
  
  /*
   * "ECMA 5-style" Javascript Object
   */
  var ObjB = Object.create(Object, {
      foo: {
          value: 0
      },
      bar: {
          value: function(value) {
              return Math.sqrt(value);
          }
      },
      _baz: {
          value: 0
      },
      baz: {
          set: function(value) {
              this._baz = value;
          },
          get: function() {
              return this._baz;
          }
      }
  });
  
  var instanceB = Object.create(ObjB);
</script>
    

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
new ObjA();
var obj = new ObjA();
pending…
Object.create(ObjB)
var obj = Object.create(ObjB);
pending…
ObjA member read/write
instanceA.foo = instanceA.foo + 1
pending…
ObjB member read/write
instanceB.foo = instanceB.foo + 1
pending…
ObjA function call
var value = instanceA.bar(64);
pending…
ObjB function call
var value = instanceB.bar(64);
pending…
ObjA "property" get/set
instanceA.setBaz(instanceA.getBaz() + 1);
pending…
ObjB property get/set
instanceB.baz = instanceB.baz + 1
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