inherit cases2

Benchmark created on


Preparation HTML

<script>
Function.prototype.create2 = function() {
    function functor() { return constructor.apply(this, args); }
    var args = Array.prototype.slice.call(arguments);
    functor.prototype = this.prototype;
    var constructor = this;
    return new functor;
};
Object.prototype.extend2 = function(extension) {
    var object = Object.create(this), property;

    for (property in extension)
        if (Object.hasOwnProperty.call(extension, property) || typeof object[property] === "undefined")
            object[property] = extension[property];

    return object;
};

var bases = [];
var inherit2 = function () {

    var body, parent, parent2, constructor;
    if (arguments.length == 1)
        body = arguments[0];

    else if (arguments.length == 2) {
        parent = inherit2.getPrototype(arguments[0]);
        body = arguments[1];
        body.prototype = parent;
        bases.unshift(parent);
        inherit2.getPrototype({a:1})
    }
    else if (arguments.length > 2) {

        parent = inherit2.getPrototype(arguments[0]);
        parent2 = inherit2.getPrototype(arguments[1]);

        body = function () {};
        body.prototype = parent;
        body.prototype = body.prototype.extend2(parent2);

        if (bases.length == 0) bases.push(parent);
        bases.push(parent2);

        Array.prototype.splice.call(arguments, 0, 2, body);
        return inherit2.apply(null, arguments);
    }

    if (bases.length > 1) constructor = body.create2.apply(body, bases);
    else constructor = (bases.length == 1) ? new body(parent) : new body;
   
    bases = [];
    constructor.init.prototype = constructor;
    return constructor.init;
}
inherit2.getPrototype = function (obj) {
  return (typeof obj === "object") ? obj : ((obj.prototype.init === undefined) ? new obj : obj.prototype);
}





</script>
<script>
var inherit = (function() {
    "use strict";

    var pt = "prototype", bases = [], body, parent, parent2, constructor, args, getprototype;

    Function.prototype.create = function() {
        function functor() { return constructor.apply(this, args); }
        var args = Array[pt].slice.call(arguments);
        functor[pt] = this[pt];
        var constructor = this;
        return new functor;
    };

    /*Object.prototype.extend = function() {
        var that = (typeof this === "object") ? this : this[pt], element, property;
        for (element in arguments)
            if (arguments[element] !== that.extend)
                for (property in arguments[element])
                    if (typeof that[property] === "undefined")
                        that[property] = arguments[element][property];

        return this;
    };*/

    Object.prototype.extend = function(extension) {
        var property, that = (typeof this === "object") ? this : this[pt];
        for (property in extension)
            if (typeof that[property] === "undefined") //With this line inherit is from right to left
                that[property] = extension[property];

        return that;
    };

    getprototype = function(obj) {
        return (typeof obj === "object") ? obj : ((typeof obj[pt].init === "undefined") ? new obj : obj[pt]);
    };

    return function() {

        args = arguments;

        if (args.length == 1)
            body = args[0];

        else if (args.length == 2) {
            parent = getprototype(args[0]);
            body = args[1];
            body[pt] = parent;
            bases.unshift(parent);
        }

        else if (args.length > 2) {

            parent = getprototype(args[0]);
            parent2 = getprototype(args[1]);

            body = function() {};
            body[pt] = parent;
            body[pt].extend(parent2);

            if (bases.length == 0) bases.push(parent);
            bases.push(parent2);

            Array[pt].splice.call(args, 0, 2, body);
            return inherit.apply(null, args);

        }

        if (bases.length > 1) constructor = body.create.apply(body, bases);
        else constructor = (bases.length == 1) ? new body(parent) : new body;
        bases = [];
        constructor.init[pt] = constructor;
        return constructor.init;
    };
}());




</script>
<script>
var inherit3 = (function() {
    "use strict";

    var pt = "prototype", bases = [], body, parent, parent2, constructor, args, getprototype;

    Function.prototype.create3 = function() {
        function functor() { return constructor.apply(this, args); }
        var args = Array[pt].slice.call(arguments);
        functor[pt] = this[pt];
        var constructor = this;
        return new functor;
    };

    Object.prototype.extend3 = function(extension) {
        var property, that = (typeof this === "object") ? this : this[pt];
        for (property in extension)
            if (typeof that[property] === "undefined") //With this line inherit is from right to left
                that[property] = extension[property];

        return that;
    };

    getprototype = function(obj) {
        return (typeof obj === "object") ? obj : ((typeof obj[pt].init === "undefined") ? new obj : obj[pt]);
    };

    return function() {

        bases = [];
        args = arguments;

        body = Array[pt].pop.call(args);

        if (args.length > 0) {
            parent = getprototype(Array[pt].shift.call(args));
            bases.push(parent);
            
            if (args.length == 0) {
                
                body[pt] = parent;
                
            }         
            else {
                //console.log(1, parent)
                var bodytemp = function(){};
                bodytemp[pt] = parent;
                for (var i in args) {
                    var newinherit = getprototype(args[i]);
                    if (i !== "extend") {
                        bodytemp[pt].extend3(newinherit);
                        bases.push(newinherit);
                    }
                }
                //console.log(2, parent)
                body[pt] = bodytemp.prototype;
            }
        }

        if (bases.length > 1) constructor = body.create3.apply(body, bases);
        else constructor = (bases.length == 1) ? new body(parent) : new body;

        constructor.init[pt] = constructor;
        return constructor.init;
    };
}());



</script>

Test runner

Ready to run.

Testing in
TestOps/sec
recursive old
var A1_2 = inherit2(function () {
    this.a1 = "a1";
    this.init = function(name) {
      this.name = name;
    };
});
var A2_2 = inherit2(A1_2, function (base) {
    this.a2 = "a2";
    this.init = function(name) {
      base.init(name);
    };
});
var A3_2 = inherit2(A2_2, function (base) {
    this.a3 = "a3";
    this.init = function(name) {
      base.init(name);
    };
});
var A4_2 = function () {
    this.a4 = "a4";
    this.init = function() {
      console.log(4)
    };
};
var A5_2 = {
    a5: "a5",
    init: function() {
      console.log(5);
    }
};
//console.log(new A2("aa"))
var B_2 = inherit2(A3_2,A4_2,A5_2,  function (base, base2, base3, base4, base5) {
    var args = arguments;
    this.b = "b";
    this.init = function(name) {
        base2.init(name);
        //console.log(this)
    };
});


var ins = new B_2("Mery"); ins
ready
recursive

var A1 = inherit(function () {
    this.a1 = "a1";
    this.init = function(name) {
      this.name = name;
    };
});
var A2 = inherit(A1, function (base) {
    this.a2 = "a2";
    this.init = function(name) {
      base.init(name);
    };
});
var A3 = inherit(A2, function (base) {
    this.a3 = "a3";
    this.init = function(name) {
      base.init(name);
    };
});
var A4 = function () {
    this.a4 = "a4";
    this.init = function() {
      console.log(4)
    };
};
var A5 = {
    a5: "a5",
    init: function() {
      console.log(5);
    }
};
//console.log(new A2("aa"))
var B = inherit(A3,A4,A5,  function (base, base2, base3, base4, base5) {
    var args = arguments;
    this.b = "b";
    this.init = function(name) {
        base2.init(name);
        //console.log(this)
    };
});

var ins = new B("Mery"); ins
ready
normal

var A1_3 = inherit3(function () {
    this.a1 = "a1";
    this.init = function(name) {
      this.name = name;
    };
});
var A2_3 = inherit3(A1_3, function (base) {
    this.a2 = "a2";
    this.init = function(name) {
      base.init(name);
    };
});
var A3_3 = inherit3(A2_3, function (base) {
    this.a3 = "a3";
    this.init = function(name) {
      base.init(name);
    };
});
var A4_3 = function () {
    this.a4 = "a4";
    this.init = function() {
      console.log(4)
    };
};
var A5_3 = {
    a5: "a5",
    init: function() {
      console.log(5);
    }
};
//console.log(new A2("aa"))
var B_3 = inherit3(A3_3,A4_3,A5_3,  function (base, base2, base3, base4, base5) {
    var args = arguments;
    this.b = "b";
    this.init = function(name) {
        base.init(name);
        //console.log(this)
    };
});
var ins = new B_3("Mery"); ins
ready

Revisions

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