new vs Object.create (v16)

Revision 16 of this benchmark created by Eric Elliott on


Preparation HTML

<script>
  var Klass = function() {};
  
  Klass.prototype.a = function() {};
  Klass.prototype.b = function() {};
  Klass.prototype.c = function() {};
  
  if (typeof Object.create !== "function") {
        Object.create = function(o) {
                function F() {}
                F.prototype = o;
                return new F();
        };
  }

  var proto = {
    a: function () {},
    b: function () {},
    c: function () {}
  }

  var literal = function () {
    return {
      a: function () {},
      b: function () {},
      c: function () {}
    };
  }

  var newFactory = function () {
    return new Klass();
  }

  var count = 10240;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
new
for (var i = 0; i < count; i++) {
  var object = new Klass();
  if (!object)
    throw new Error();
}
ready
Object.create
for (var i = 0; i < count; i++) {
  var object = Object.create(proto);
  if (!object)
    throw new Error();
}
ready
Object literal function call
for (var i = 0; i < count; i++) {
  var object = literal();
  if (!object)
    throw new Error();
}
ready
Object literal in loop
for (var i = 0; i < count; i++) {
  var object = {
    a: function () {},
    b: function () {},
    c: function () {}
  };
  if (!object)
    throw new Error();
}
ready
new factory
for (var i = 0; i < count; i++) {
  var object = newFactory();
  if (!object)
    throw new Error();
}
ready

Revisions

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