different classes oop (v5)

Revision 5 of this benchmark created on


Preparation HTML

<script>
var augment = (function (bind, call) {
    "use strict";

    var bindable = Function.bindable = bind.bind(bind);
    var callable = Function.callable = bindable(call);

    var arrayFrom = Array.from = callable(Array.prototype.slice);
    var ownPropertyOf = Object.ownPropertyOf = callable(Object.hasOwnProperty);

    //Object.defineProperty(Function.prototype, "augment", augmentDescriptor);
    Object.defineProperty(Object.prototype, "augment", { value: augment });

    return callable(augment);

    function augment(body) {
        var base = typeof this === "function" ? this.prototype : this;
        var prototype = Object.create(base);
        body.apply(prototype, arrayFrom(arguments, 1).concat(base));
        if (!ownPropertyOf(prototype, "constructor")) return prototype;
        var constructor = prototype.constructor;
        constructor.prototype = prototype;
        return constructor;
    }
}(Function.bind, Function.call));
</script>
<script>
function inherit() {
    var len = arguments.length;
    var parent = (len > 1) ? arguments[0] : Function.constructor;
    var body = arguments[len - 1];
    body.prototype = parent.prototype;

    var constructor = new body(parent.prototype);
    constructor.init.prototype = constructor;
    return constructor.init;
}
function inherit2() {
    var len = arguments.length;
    var parent = (len > 1) ? arguments[0] : Function.constructor;
    var body = arguments[len - 1];
    body.prototype = parent.prototype;

    var t = function (){
        if (this.init)
            this.init.apply(this, arguments);
    };
    t.prototype = new body(parent.prototype);
    return t;
}
</script>

Setup

var InheritPerson = inherit(function () {
            this.init = function(name) {
                this.nome = name;
            };
            this.setAddress = function(country, city, street) {
                  this.country = country;
                  this.city = city;
                  this.street = street;
            };
        });
    
        var InheritFrenchGuy = inherit(InheritPerson, function(base) {
            
                this.init = function (name) {
                  base.init.call(this,name);
                };
            
                this.setAddress = function(city, street) {
                  base.setAddress.call(this,"France", city, street);
                };
        });
    
        var InheritParisLover = inherit(InheritFrenchGuy, function(base) {
            
            this.init= function (name) {
                base.init.call(this,name);
            };
            
            this.setAddress = function(street) {
                base.setAddress.call(this,"Paris", street);
                return this;
            };
        });
    
    var InheritPerson2 = inherit2(function () {
            this.init = function(name) {
                this.nome = name;
            };
            this.setAddress = function(country, city, street) {
                  this.country = country;
                  this.city = city;
                  this.street = street;
            };
        });
    
        var InheritFrenchGuy2 = inherit2(InheritPerson2, function(base) {
            
                this.init = function (name) {
                  base.init.call(this,name);
                };
            
                this.setAddress = function(city, street) {
                  base.setAddress.call(this,"France", city, street);
                };
        });
    
        var InheritParisLover2 = inherit(InheritFrenchGuy2, function(base) {
            
            this.init= function (name) {
                base.init.call(this,name);
            };
            
            this.setAddress = function(street) {
                base.setAddress.call(this,"Paris", street);
                return this;
            };
        });
    
        var AugmentPerson = Object.augment(function() {
            this.constructor = function (name) {
              this.nome = name;
            };
        
            this.setAddress = function(country, city, street) {
              this.country = country;
              this.city = city;
              this.street = street;
            };
          });
        
          var AugmentFrenchGuy = AugmentPerson.augment(function(base) {
        
            this.constructor = function (name) {
              base.constructor.call(this,name);
            };
        
            this.setAddress = function(city, street) {
              base.setAddress.call(this,"France", city, street);
            };
          });
        
          var AugmentParisLover = AugmentFrenchGuy.augment(function(base) {
        
            this.constructor = function (name) {
              base.constructor.call(this,name);
            };
        
            this.setAddress = function(street) {
              base.setAddress.call(this,"Paris", street);
              return this;
            };
          });

Test runner

Ready to run.

Testing in
TestOps/sec
inherit.js
var t = new InheritParisLover("Mary");
t.setAddress("CH");
ready
augment.js
var t = new AugmentParisLover("Mary");
t.setAddress("CH");
ready
inherit2
var t = new InheritParisLover2("Mary");
t.setAddress("CH");
ready

Revisions

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