Object.create vs. new

JavaScript performance comparison

Revision 13 of this test case created by

Preparation code

<script>
  Object.defineProperties(Object.prototype, {
    _create: {
      value: function(state) {
        var This = this.constructor;
        if (This.prototype !== this && this.hasOwnProperty('constructor')) This.prototype = this;
        return new This(state);
      },
      writable: true,
      configurable: true
    },
    _create2: {
      value: function(state) {
        var obj = state;
        obj.__proto__ = this;
        return obj;
      },
      writable: true,
      configurable: true
    },
    constructor: {
      value: function(state) {
        for (var name in state) this[name] = state[name]
      },
      writable: true,
      configurable: true
    }
  });
object$ = Object.prototype;
Object.create2 = function(proto){
    var obj = {};
    obj.__proto__ = proto;
    return obj;
};
(function () {
    var privateKey = {};
    Object.defineProperty(Object.prototype, 'clone', {
     value: function () {
        if (this[privateKey] == null) {
            this[privateKey] = function () {};
            this[privateKey].prototype = this;
        }
        return new (this[privateKey]());
    }});
})();
</script>
      
<script>
Benchmark.prototype.setup = function() {
  function MyClass(x, y) {
    this.x = x;
    this.y = y;
  }
  
  var MyProtoClass = {
    init: (function(x, y) {
      this.x = x;
      this.y = y;
      return this;
    }),
    x: 0,
    y: 0
  };
  
  function doSomething(obj) {
    var x = obj.x + obj.y,
        y = obj.x * obj.y,
        z = obj.x - obj.y;
  
    return x * y * z;
  }
  
  var x = 0 | (Math.random() * 10000),
      y = 0 | (Math.random() * 10000);
  
  function copy(from) {
    var key;
    var keys = Object.getOwnPropertyNames(from);
    var to = {};
    var i = 0;
    while (typeof (key = keys[i++]) !== 'undefined') {
      to[key] = from[key];
    }
    return to;
  }

};
</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 operator
var obj = new MyClass(x, y);
doSomething(obj);
pending…
Object.create().init()
var obj = Object.create(MyProtoClass).init(x, y);
doSomething(obj);
pending…
Object.create()
//See if it might be faster by not calling the init function.
var obj = Object.create(MyProtoClass);
obj.x = x;
obj.y = y;
doSomething(obj);
pending…
prototype._create()
var obj = object$._create({
  x: x,
  y: y
});
doSomething(obj);
pending…
prototype._create2() by __proto__
var obj = object$._create2({
  x: x,
  y: y
});
doSomething(obj);
pending…
Object.create2 by __proto__
var obj = Object.create2(MyProtoClass);
obj.x = x;
obj.y = y;
doSomething(obj);
pending…
Copy from object to object
var obj = copy(MyProtoClass);
obj.x = x;
obj.y = y;
doSomething(obj);
pending…
Object.prototype.clone with new oprerator
var obj = MyProtoClass.clone();
obj.x = x;
obj.y = y;
doSomething(obj);
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