Inherit method with caller vs fn wrapper

Benchmark created by Andy on


Setup

superUseCaller = (function() {
       var callSuper = function(){
          var caller = callSuper.caller
          return caller._super.apply(this, arguments);
       }
    
       function Parent () {
          this.i=0;
       }
       Parent.prototype.methodA = function() {
          this.i++
       }
       
       function Child() {
          this.i=0;
       }
       Child.prototype = new Parent();
       Child.prototype._super = callSuper;
       Child.prototype.methodA = function() {
          this.i++;
          this._super();
       }
       Child.prototype.methodA._super = Parent.prototype.methodA;
       
       return {
          Parent: Parent,
          Child: Child
       }   
    })();
    
    superUseInject = (function() {
       "use strict"
       function emptyFn() {}
       function isFunction(obj) {
                return Object.prototype.toString.call(obj) === "[object Function]";
            }
    
       var injectSuper = function(ancestor, name, fn){
    
          function _injected() {
             this._super = ancestor[name];
    
             // fail-over incase ancestor has no such method
             if (!isFunction(this._super)) {
                this._super = emptyFn;
             }
    
             return fn.apply(this, arguments);
          }
    
          return _injected;
       }
    
       function Parent () {
          this.i=0;
       }
       Parent.prototype.methodA = function() {
          this.i++;
       }
       
       function Child() {
          this.i=0;
       }
       Child.prototype = new Parent();
       Child.prototype.methodA = function() {
          this.i++;
          this._super();
       }
       Child.prototype["methodA"] = injectSuper(Parent, "methodA", Child.prototype.methodA);
       
       return {
          Parent: Parent,
          Child: Child
       }   
    })();

Test runner

Ready to run.

Testing in
TestOps/sec
Use Caller
var child = new superUseCaller.Child();
child.methodA();
ready
Use Inject
var child = new superUseInject.Child();
child.methodA();
ready

Revisions

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

  • Revision 1: published by Andy on