on-the-fly variadic native constructors (warning: really gross)

JavaScript performance comparison

Test case created by somebody who's to embarrassed to put their name on something this horrifying

Info

ew no. don't do this.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    window.createWithSharedCache = (function() {
      var cache = {};
      return function(ctor, args) {
        var factory;
        var arity = args.length;
        var key = "" + ctor.name + ":" + arity;
       
        if (!(factory = cache[key])) {
          var argNames = Array(arity);
          for (var i = 0; i < arity; ++i)
            argNames[i] = "$" + i;
          cache[key] = factory = Function('ctor', argNames, "return new ctor(" + argNames + ")");
        }
        return factory.apply(null, [ctor].concat(args));
      };
    })();
   
    window.createWithCachePerConstructor = function(ctor, args) {
      var cache;
      var arity = args.length;
     
      if (!(cache = ctor.__cno_factories__))
        cache = ctor.__cno_factories__ = {};
     
      if (!(factory = cache[arity])) {
        var argNames = Array(arity);
        for (var i = 0; i < arity; ++i)
          argNames[i] = "$" + i;
        cache[arity] = factory = Function('ctor', argNames, "return new ctor(" + argNames + ")");
      }
      return factory.apply(null, [ctor].concat(args));
    };
   
    window.createWithoutCache = function(ctor, args) {
      var arity = args.length
      var argNames = Array(arity)
      for (var i = 0; i < arity; ++i)
        argNames[i] = "$" + i;
      return Function('ctor', argNames, "return new ctor(" + argNames + ")").apply(null, [ctor].concat(args));
    };
   
};
</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
with shared factory function cache
createWithSharedCache(Array, ['a', 'b', 'c', 'd']);
createWithSharedCache(Array, [1, 2, 3, 4, 5, 6]);
pending…
with per-constructor factory function cache
createWithCachePerConstructor(Array, ['a', 'b', 'c', 'd']);
createWithCachePerConstructor(Array, [1, 2, 3, 4, 5, 6]);
pending…
without cache
createWithoutCache(Array, ['a', 'b', 'c', 'd']);
createWithoutCache(Array, [1, 2, 3, 4, 5, 6]);
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