Konstruct

JavaScript performance comparison

Test case created by John Watson

Info

Test direct args vs Konstructor nonsense

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var kPooler = function() {
      var Klass = this;
      if (Klass.instancePool.length) {
        var instance = Klass.instancePool.pop();
        Klass.call(instance, arguments);
        return instance;
      }
      if (!Klass.construct) {
        function Konstructor(args) {
          return Klass.apply(this, args);
        }
        Konstructor.prototype = Klass.prototype;
        Klass.construct = function(args) {
          return new Konstructor(args);
        }
      }
      return Klass.construct(arguments);
    };
   
    var fiveArgumentPooler = function(a1, a2, a3, a4, a5) {
      var Klass = this;
      if (Klass.instancePool.length) {
        var instance = Klass.instancePool.pop();
        Klass.call(instance, a1, a2, a3, a4, a5);
        return instance;
      } else {
        return new Klass(a1, a2, a3, a4, a5);
      }
    }
   
    function Foo(a,b,c,d,e) {
      this.a = a;
      this.b = b;
      this.c = c;
      this.d = d;
      this.e = e;
    }
   
    Foo.kp = kPooler;
    Foo.fp = fiveArgumentPooler;
    Foo.instancePool = []; // not really used - here other than checking length
};
</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
direct
Foo.fp(1,2,3,4,5);
pending…
konstruct!
Foo.kp(1,2,3,4,5);
pending…

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

Compare results of other browsers

0 comments

Add a comment