new vs create

JavaScript performance comparison

Revision 4 of this test case created by herby

Preparation code

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
new
var test = new Ctor;
pending…
create
var test = Object.create(Create);
pending…
factory
var test = Factory();
pending…
literal
var test = Literal();
pending…
homebrew create
var test = HomebrewCreate(Create);
pending…
Ctor with re-set .prototype
var test = new Ctor2;
pending…
Ctor with re-linked prototype
var test = new Ctor3;
pending…
Homebrew create based on existing prototype
var test = HomebrewCreate(Ctor.prototype);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment