Object.create vs. new

JavaScript performance comparison

Revision 15 of this test case created by Matt Bierner

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var create = Object.create;
  
  var MyClass = function(x, y) {
    this.x = x;
    this.y = y;
  }
  
  var Factory = function(x, y) {
      return { 'x': x, 'y': y }
  };
  
  var CreateFactory = function(x, y) {
  return create(null, {
  'x': {value: x},
  'y': {value: y}
  });
  };
  
  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);
  

};
</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…
Factory
var obj = Factory(x, y);
doSomething(obj);
pending…
Object.create()
var obj = CreateFactory(x, y);
doSomething(obj);
pending…
create()
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