Proto vs Ctor

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var ProtoObject = Object.create(Object);
  
  ProtoObject.extend = function(props) {
    var prop = null;
    for (prop in props) {
      if (props.hasOwnProperty(prop)) {
        this[prop] = props[prop];
      }
    }
  };
  
  ProtoObject.clone = function(props) {
    var newObj = Object.create(this);
    newObj.extend(props);
    return newObj;
  };
   
  var ProtoCustomer = ProtoObject.clone({
    id: 0,
    getId: function() {
      return this.id;
    }
  });
  
  ProtoCustomer2 = Object.create(Object);
  ProtoCustomer2.id = 0;
  ProtoCustomer2.getId = function() {
    return this.id;
  };
  
  function CtorBase() {}
  
  function CtorCustomer(id) {
    this.id = id;
  }
  CtorCustomer.prototype = new CtorBase()
  CtorCustomer.prototype.getId = function() { 
    return this.id; 
  };
  

};
</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
Proto
var cust = ProtoCustomer.clone({ id: 100 });
cust.getId();
pending…
Ctor Cust
var cust = new CtorCustomer(100);
cust.getId();
pending…
Raw proto
var cust = Object.create(ProtoCustomer2);
cust.id = 100;
cust.getId();
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