new vs create (v4)

Revision 4 of this benchmark created by herby on


Preparation HTML

<script>
  var Ctor = function() {};
  Ctor.prototype.variable = 'test';
  Ctor.prototype.method = function() {
   return this.variable;
  };
  
  var Create = {
   variable: 'test',
   method: function() {
    return this.variable;
   }
  };
  
  var Literal = function() {
   return {
    variable: 'test',
    method: function() {
     return this.variable;
    }
   };
  };
  
  (function() {
   var variable = 'test';
   window.Factory = function() {
    return {
     variable: variable,
     method: function() {
      return this.variable;
     }
    };
   };
  }());
  
  var HomebrewCreate = function(p) {
   function f() {}
   f.prototype = p;
   return new f();
  }
  
  
  var Ctor2 = function() {};
  Ctor2.prototype = {
   variable: 'test',
   method: function() {
    return this.variable;
   }
  };
  
  var Ctor3 = function() {};
  Ctor3.prototype = Ctor.prototype;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
new
var test = new Ctor;
ready
create
var test = Object.create(Create);
ready
factory
var test = Factory();
ready
literal
var test = Literal();
ready
homebrew create
var test = HomebrewCreate(Create);
ready
Ctor with re-set .prototype
var test = new Ctor2;
ready
Ctor with re-linked prototype
var test = new Ctor3;
ready
Homebrew create based on existing prototype
var test = HomebrewCreate(Ctor.prototype);
ready

Revisions

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