Object.create vs. new (v2)

Revision 2 of this benchmark created by Alex Shvets on


Preparation HTML

<script>
Object.defineProperty(Object.prototype, '_create', {value: function(a,b,c,d,e,f){
    var Class = this.constructor;
    if(Class.prototype !== this) Class.prototype = this;
    return new Class(a,b,c,d,e,f);
}});
</script>

Setup

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
  };
  
  var MyProtoConstructorClass = {
  	constructor: (function (x, y) {
  		this.x = x;
  		this.y = y;
  		return this;
  	}),
  	x: 0,
  	y: 0
  };
  MyProtoConstructorClass.constructor.prototype = MyProtoConstructorClass;
  
  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);

Test runner

Ready to run.

Testing in
TestOps/sec
new operator
var obj = new MyClass(x, y);
doSomething(obj);
ready
Object.create().init()
var obj = Object.create(MyProtoClass).init(x, y);
doSomething(obj);
ready
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);
ready
MyProtoConstructorClass._create()
var obj = MyProtoConstructorClass._create(x, y);
doSomething(obj);
ready
Object.create().constructor()
var obj = Object.create(MyProtoConstructorClass).constructor(x, y);
doSomething(obj);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.