Prototype vs Module pattern performance

JavaScript performance comparison

Revision 157 of this test case created

Preparation code

<script type="text/javascript">
  function TraditionalPrototypeClass() {}
  TraditionalPrototypeClass.prototype.foo = function() {
    return 'foo';
  };
  TraditionalPrototypeClass.prototype.bar = function() {
    return 'bar';
  };

  function TraditionalClass() {

    this.foo = function() {
      return 'foo';
    };

    this.bar = function() {
      return 'bar';
    };

  }

  function ConstructOther() {
    //often seen in frameworks
    // constructor, but creates other object
    // this one is to be compared with traditional and literal versions
    return {
      foo: function() {
        return 'foo';
      },
      bar: function() {
        return 'bar';
      }
    };
  }

 
</script>
<script>
Benchmark.prototype.setup = function() {
    var o;
   
};
</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
Traditional Prototypal Class
o = new TraditionalPrototypeClass();
pending…
Traditional Class
o = new TraditionalClass();
pending…
Object Literal
o ={

  foo: function() {
    return 'a';
  },
  bar: function() {
    return 'b';
  }
}
pending…
ConstructOther
o = new ConstructOther();
 
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