factory-vs-constructor-pref

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function MyClass(id) {
      this.id = id;
    }
    MyClass.prototype.myMethod = function() {
      return this.id;
    }
   
    function myClass(id) {
      return {
        id: id,
        myMethod: function() {
          return this.id;
        }
      }
    }
};
</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
constructor
for (var i = 0; i < 8; i++) {
  new MyClass(i);
}
pending…
factory
for (var i = 0; i < 8; i++) {
  myClass(i);
}
pending…
literal
for (var i = 0; i < 8; i++) {
  {
    id: i,
    myMethod: function() {
      return this.id;
    }
  }
}
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