Constructor Patterns

JavaScript performance comparison

Test case created by Josh

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function A(bar) {
       this.bar = bar;
    }
   
    A.prototype = {
       getter: function() {
          return this.getter2();
       },
       getter2: function() {
          return this.bar;
       }
    }
   
   
    function B(bar) {
       function getter() {
          return getter2();
       }
       function getter() {
          return bar;
       }
   
       return getter;
    }
   
   
};
</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
Class based
var a = new A();
a.getter();
pending…
Closure based
var b = B();
b();
pending…
Closure with new
var c = new B();
c();
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