superclass vs __base

Benchmark created by wtfil on


Preparation HTML

<script>
var A, B, C, b, c;

function inheritPrototype(Parent, extend) {
    function F() {};
    function Child() {};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.prototype.superclass = Parent.prototype;
    Object.keys(extend).forEach(function (key) {
        Child.prototype[key] = extend[key];
    });
    return Child;
};


function inheritBase(Parent, extend) {
    function Child() {};
    Object.keys(extend).forEach(function (key) {
        var baseMethod = Parent.prototype[key];
        Child.prototype[key] = function () {
            var baseSaved = this.__base,
                result;
            this.__base = baseMethod;
            result = extend[key].apply(this, arguments);
            this.__base = baseSaved;
            return result;
        }
    });
    return Child;
}


A = function () {};
        
A.prototype = {
    getRandom: function () {
        return 4;
    }
}   
        
B = inheritPrototype(A, {
    getRandom: function () {
        return this.superclass.getRandom();
    }
}); 
        
C = inheritBase(A, {
    getRandom: function () {
        return this.__base();
    }
})

b = new B();
c = new C();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
superclass
b.getRandom();
ready
__base
c.getRandom();
ready

Revisions

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

  • Revision 1: published by wtfil on