Prototype inheritance approaches

Benchmark created by Joshua Koudys on


Setup

var methods = {
      foo: function foo() { return 1; },
      bar: function bar() { return 1 + 1; },
      baz: function baz() { var a = 123; }
    };
    
    function propify(props) {
      return Object.keys(methods).reduce(function(p, k) {
        p[k] = {value: props[k]};
        return p;
      }, {});
    }
    
    var methodProps = propify(methods);
    
    function FirstClass() {}
    function SecondClass() {
      FirstClass.call(this);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Assign to prototype directly
Object.assign(SecondClass.prototype, FirstClass.prototype, methods);
ready
Create from prototype
SecondClass.prototype = Object.create(FirstClass.prototype, methodProps);
ready
Assign from new
SecondClass.prototype = Object.assign(new FirstClass, methods);
ready

Revisions

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

  • Revision 1: published by Joshua Koudys on