Comparing inherit v2 (v7)

Revision 7 of this benchmark created by Enzo on


Setup

var inherit = function() {
        'use strict';
    
        var o = 'object',
                p = 'prototype',
                c = 'constructor',
                newfunction = function(){},
                args = arguments,
                parent = args[0],
                bodyclass = (args.length == 1) ? newfunction : args[1],
                parentisobject = typeof parent === o,
                body,
                newproto,
                hasconstructor;
    
        if (typeof bodyclass === o) {
                body = newfunction;
                body[p] = bodyclass;
        }
        else {
                body = bodyclass;
                body[p] = (parentisobject) ? parent : new parent();
        }
    
        newproto = new body(body[p]);
        hasconstructor = !newproto.hasOwnProperty(c);
        if ( (hasconstructor && parent === newproto[c]) || (hasconstructor && parentisobject) )
                newproto[c] = newfunction;
    
        newproto[c][p] = newproto;
        return newproto[c];
    
    };
    
    var inherit2 = function() {
        'use strict';
    
        var o = 'object',
            p = 'prototype',
            c = 'constructor',
            parent = arguments[0],
            bodyclass = (arguments.length == 1) ? function(){} : arguments[1],
            parentisobject = typeof parent === o,
            body,
            newproto,
            hasconstructor;
    
        if (typeof bodyclass === o) {
            body = function(){};
            body.prototype = bodyclass;
        }
        else {
            body = bodyclass;
            body.prototype = (parentisobject) ? parent : new parent();
        }
    
        newproto = new body(body.prototype);
        if ( (!newproto.hasOwnProperty(c) && parent === newproto.constructor) || (!newproto.hasOwnProperty(c) && parentisobject) )
            newproto.constructor = function(){};
    
        newproto.constructor.prototype = newproto;
        return newproto.constructor;
    
    };
    
    var inherit3 = function() {
        'use strict';
    
        var o = 'object',
                p = 'prototype',
                c = 'constructor',
                newfunction = new Function,
                args = arguments,
                parent = args[0],
                bodyclass = (args.length == 1) ? newfunction : args[1],
                parentisobject = typeof parent === o,
                body,
                newproto,
                hasconstructor;
    
        if (typeof bodyclass === o) {
                body = newfunction;
                body[p] = bodyclass;
        }
        else {
                body = bodyclass;
                body[p] = (parentisobject) ? parent : new parent();
        }
    
        newproto = new body(body[p]);
        hasconstructor = !newproto.hasOwnProperty(c);
        if ( (hasconstructor && parent === newproto[c]) || (hasconstructor && parentisobject) )
                newproto[c] = newfunction;
    
        newproto[c][p] = newproto;
        return newproto[c];
    
    };
    
    var inherit22 = function() {
        'use strict';
    
        function newfunction(){}
    
        var o = 'object',
                p = 'prototype',
                c = 'constructor',
                args = arguments,
                parent = ( args.length > 1 ) ? args[0] : newfunction,
                body = args[ args.length-1 ],
                new_proto;
    
    
        body[p] = Object.create( parent[p] ),
        new_proto = new body( body[p] );
    
        if ( !new_proto.hasOwnProperty(c) && parent === new_proto[c] )
                new_proto[c] = newfunction;
    
        new_proto[c][p] = new_proto;
        return new_proto[c];
    
    };
    
    var inherit1 = function() {
        'use strict';
        var len = arguments.length,
                parent = (len > 1) ? arguments[0] : function(){},
                body = arguments[len - 1];
    
        body.prototype = Object.create(parent.prototype);
        var prototype = new body(body.prototype);
        prototype.constructor.prototype = prototype;
        return prototype.constructor;
    };
    
    var inheriten = (function() {
        'use strict';
    
        var o = 'object',
            p = 'prototype',
            c = 'constructor',
            args,
            parent,
            bodyclass,
            newfunction,
            parentisobject,
            body,
            newproto,
            hasconstructor;
    
        return function() {
    
            newfunction = function(){},
            args = arguments,
            parent = args[0],
            bodyclass = (args.length == 1) ? newfunction : args[1],
            parentisobject = typeof parent === o;
    
            if (typeof bodyclass === o) {
                body = newfunction;
                body[p] = bodyclass;
            }
            else {
                body = bodyclass;
                body[p] = (parentisobject) ? parent : new parent();
            }
    
            newproto = new body( body[p] );
            hasconstructor = !newproto.hasOwnProperty(c);
            if ( (hasconstructor && parent === newproto[c]) || (hasconstructor && parentisobject) )
                newproto[c] = newfunction;
    
            newproto[c][p] = newproto;
            return newproto[c];
    
        }
    
    })();

Test runner

Ready to run.

Testing in
TestOps/sec
released
var InheritPerson = inherit(function () {
        this.constructor = function(name) {
            this.name = name;
        };
        this.setAddress = function(country, city, street) {
            this.country = country;
            this.city = city;
            this.street = street;
        };
    });
    
    var InheritFrenchGuy = inherit(InheritPerson, function(base) {
            this.constructor = function (name) {
              base.constructor.call(this, name);
            };
       
            this.setAddress = function(city, street) {
              base.setAddress.call(this, "France", city, street);
            };
    });
    
    var InheritParisLover = inherit(InheritFrenchGuy, function(base) {
        this.constructor= function (name) {
            base.constructor.call(this, name);
        };
       
        this.setAddress = function(street) {
            base.setAddress.call(this, "Paris", street);
            return this;
        };
    });

var t1 = new InheritPerson("John");
t1.setAddress("US", "MT", "CH");

var t2 = new InheritFrenchGuy("Leo");
t2.setAddress("MT", "CH");

var t3 = new InheritParisLover("Mary");
t3.setAddress("CH");
ready
no alias
var InheritPerson2 = inherit2(function () {
        this.constructor = function(name) {
            this.name = name;
        };
        this.setAddress = function(country, city, street) {
            this.country = country;
            this.city = city;
            this.street = street;
        };
    });
    
    var InheritFrenchGuy2 = inherit2(InheritPerson2, function(base) {
            this.constructor = function (name) {
              base.constructor.call(this, name);
            };
       
            this.setAddress = function(city, street) {
              base.setAddress.call(this, "France", city, street);
            };
    });
    
    var InheritParisLover2 = inherit(InheritFrenchGuy2, function(base) {
        this.constructor= function (name) {
            base.constructor.call(this, name);
        };
       
        this.setAddress = function(street) {
            base.setAddress.call(this, "Paris", street);
            return this;
        };
    });

var t1 = new InheritPerson2("John");
t1.setAddress("US", "MT", "CH");

var t2 = new InheritFrenchGuy2("Leo");
t2.setAddress("MT", "CH");

var t3 = new InheritParisLover2("Mary");
t3.setAddress("CH");
ready
new Function
var InheritPerson3 = inherit3(function () {
        this.constructor = function(name) {
            this.name = name;
        };
        this.setAddress = function(country, city, street) {
            this.country = country;
            this.city = city;
            this.street = street;
        };
    });
    
    var InheritFrenchGuy3 = inherit3(InheritPerson3, function(base) {
            this.constructor = function (name) {
              base.constructor.call(this, name);
            };
       
            this.setAddress = function(city, street) {
              base.setAddress.call(this, "France", city, street);
            };
    });
    
    var InheritParisLover3 = inherit(InheritFrenchGuy3, function(base) {
        this.constructor= function (name) {
            base.constructor.call(this, name);
        };
       
        this.setAddress = function(street) {
            base.setAddress.call(this, "Paris", street);
            return this;
        };
    });

var t1 = new InheritPerson3("John");
t1.setAddress("US", "MT", "CH");

var t2 = new InheritFrenchGuy3("Leo");
t2.setAddress("MT", "CH");

var t3 = new InheritParisLover3("Mary");
t3.setAddress("CH");
ready
inherit 1.0 (lite)
var InheritPerson1 = inherit1(function () {
        this.constructor = function(name) {
            this.name = name;
        };
        this.setAddress = function(country, city, street) {
            this.country = country;
            this.city = city;
            this.street = street;
        };
    });
    
    var InheritFrenchGuy1 = inherit1(InheritPerson1, function(base) {
            this.constructor = function (name) {
              base.constructor.call(this, name);
            };
       
            this.setAddress = function(city, street) {
              base.setAddress.call(this, "France", city, street);
            };
    });
    
    var InheritParisLover1 = inherit1(InheritFrenchGuy1, function(base) {
        this.constructor= function (name) {
            base.constructor.call(this, name);
        };
       
        this.setAddress = function(street) {
            base.setAddress.call(this, "Paris", street);
            return this;
        };
    });

var t1 = new InheritPerson1("John");
t1.setAddress("US", "MT", "CH");

var t2 = new InheritFrenchGuy1("Leo");
t2.setAddress("MT", "CH");

var t3 = new InheritParisLover1("Mary");
t3.setAddress("CH");
ready
encapsuled
var InheritPersonen = inheriten(function () {
        this.constructor = function(name) {
            this.name = name;
        };
        this.setAddress = function(country, city, street) {
            this.country = country;
            this.city = city;
            this.street = street;
        };
    });
    
    var InheritFrenchGuyen = inheriten(InheritPersonen, function(base) {
            this.constructor = function (name) {
              base.constructor.call(this, name);
            };
       
            this.setAddress = function(city, street) {
              base.setAddress.call(this, "France", city, street);
            };
    });
    
    var InheritParisLoveren = inheriten(InheritFrenchGuyen, function(base) {
        this.constructor= function (name) {
            base.constructor.call(this, name);
        };
       
        this.setAddress = function(street) {
            base.setAddress.call(this, "Paris", street);
            return this;
        };
    });

var t1 = new InheritPersonen("John");
t1.setAddress("US", "MT", "CH");

var t2 = new InheritFrenchGuyen("Leo");
t2.setAddress("MT", "CH");

var t3 = new InheritParisLoveren("Mary");
t3.setAddress("CH");
ready
v2.2
var InheritPerson22 = inherit22(function () {
        this.constructor = function(name) {
            this.name = name;
        };
        this.setAddress = function(country, city, street) {
            this.country = country;
            this.city = city;
            this.street = street;
        };
    });
    
    var InheritFrenchGuy22 = inherit22(InheritPerson22, function(base) {
            this.constructor = function (name) {
              base.constructor.call(this, name);
            };
       
            this.setAddress = function(city, street) {
              base.setAddress.call(this, "France", city, street);
            };
    });
    
    var InheritParisLover22 = inherit22(InheritFrenchGuy22, function(base) {
        this.constructor= function (name) {
            base.constructor.call(this, name);
        };
       
        this.setAddress = function(street) {
            base.setAddress.call(this, "Paris", street);
            return this;
        };
    });

var t1 = new InheritPerson22("John");
t1.setAddress("US", "MT", "CH");

var t2 = new InheritFrenchGuy22("Leo");
t2.setAddress("MT", "CH");

var t3 = new InheritParisLover22("Mary");
t3.setAddress("CH");
ready

Revisions

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