Dynamic arguments to the constructor (v5)

Revision 5 of this benchmark created by tenbits on


Description

Test which way of creation object by given constructor and ARRAY of arguments are faster: + Fake constructor creation; + Function.prototype.bind method

Setup

function MyClass(a,b,c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    
    function create1(ctor, args){
      function Service(){};
      Service.prototype = ctor.prototype;
      var s = new Service();
      ctor.apply(s, args);
      return s;
    }
    
    function create2(ctor, args) {
        return new (Function.prototype.bind.apply(ctor, [null].concat(args)));
    }
    
    function create3(ctor, args) {
            var i = Object.create(ctor.prototype)
              , r = ctor.apply(i, args);
            return r != null && (typeof r == 'object' || typeof r == 'function') ? r : i;
        }

Test runner

Ready to run.

Testing in
TestOps/sec
Fake constructor
create1(MyClass, [1,2,3])
ready
Function.prototype.bind
create2(MyClass, [1,2,3])
ready
Object.create (pretty similar to Fake constructor)
create3(MyClass, [1,2,3])
ready
new
new MyClass(1, 2, 3)
ready

Revisions

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