different classes oop (v22)

Revision 22 of this benchmark created on


Description

Added - True prototype (native) - lineage.js

Preparation HTML

<script src="http://dl.dropbox.com/u/7677927/oop-benchmark/lib/jrclass.js"></script>
<script src="http://raptorjs.org/dist/raptor.min.js" type="text/javascript"></script>
<script src="http://indigounited.com/dejavu/dejavu.js"></script>
<script src="http://dl.dropbox.com/u/7677927/oop-benchmark/lib/my.class.js"></script>
<script src="https://rawgithub.com/tnhu/jsface/v2.1.1/jsface.js"></script>
<script type="text/javascript" src="http://lineagejs.googlecode.com/files/lineage-0.4.1.js"></script>
<script>

function inherit() {
    var len = arguments.length;
    var parent = (len > 1) ? arguments[0] : function(){};
    var body = arguments[len - 1];

    body.prototype = parent.prototype;
    var constructor = new body(parent.prototype);
     if (constructor.init === undefined) constructor.init = function(){};
    constructor.init.prototype = constructor;
    return constructor.init;
}
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));

Function.prototype.new = 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.extend = function (extension) {
      var hasOwnProperty = Object.hasOwnProperty;
      var object = Object.create(this);
  
      for (var property in extension)
          if (hasOwnProperty.call(extension, property) ||
              typeof object[property] === "undefined")
                  object[property] = extension[property];
 
     return object;
};

var inherit2 = (function() {
    "use strict";

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

    Function.prototype.create2 = 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.extend2 = 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]);
            if (bases.length == 0) bases.push(({}).extend2(parent));

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

            bases.push(parent2);

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

        }

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


<script>
(function(){function u(){}function f(){}var i=typeof global!="undefined",r,o,y;String.prototype.format||(r={},String.prototype.format=function(){for(var t=this,i=arguments,n=i.length;--n>=0;)t=t.replace(r[n]||(r[n]=RegExp("\\{"+n+"\\}","gm")),i[n]);return t},String.format||(String.format=function(n){for(var i=arguments,t=i.length;--t;)n=n.replace(r[t-1]||(r[t-1]=RegExp("\\{"+(t-1)+"\\}","gm")),i[t]);return n}));var t=Function.prototype,l=Array.prototype,e=l.forEach,a=Object.defineProperty,v=typeof Object.defineProperties=="function",n={};if(n=n.__proto__===Object.prototype,n)try{n={},n.__proto__={Object:1},n=n.Object===1}catch(b){n=!1}if(typeof Object.getPrototypeOf!="function"&&(Object.getPrototypeOf=n?function(n){return n.__proto__}:function(n){return n==null||!n.constructor||n===Object.prototype?null:n.constructor.prototype===n?Object.prototype:n.constructor.prototype}),e||(l.forEach=e=function(n,t){for(var i=0,r=this.length;i<r;++i)n.call(t||this,this[i],i,this)}),t.fastClass=function(n){var f=this,e=this.prototype,u,r;return n=n||function(){this.constructor=function(){f.apply(this,arguments)}},n.prototype=e,u=new n(e,this),u.hasOwnProperty("constructor")||(u.constructor=function(){f.apply(this,arguments)}),r=u.constructor,r.prototype=u,n=null,arguments.length>1&&t.define.apply(r,arguments),r.constructor=r,r},t.inheritWith=n?function(n){var u=this,r=(typeof n=="function"?n.call(this,this.prototype,this):n)||{},i=r.hasOwnProperty("constructor")?r.constructor:function(){u.apply(this,arguments)};return i.prototype=r,r.__proto__=this.prototype,n=null,arguments.length>1&&t.define.apply(i,arguments),i.constructor=i,i}:function(n){var e=this,r=(typeof n=="function"?n.call(this,this.prototype,this):n)||{},f=r.hasOwnProperty("constructor")?r.constructor:function(){e.apply(this,arguments)},o;return u.prototype=this.prototype,f.prototype=o=new u,n=r,t.define.apply(f,arguments),f.constructor=f,f},t.define=function(n,t){var f=this,r=this.prototype,s=n,o;if(n){typeof n=="function"&&(n=n.call(r,this.prototype,this));for(o in n)r[o]=n[o]}return n=null,(arguments.length>2||t)&&(r.hasOwnProperty("__mixins__")||(v?a(r,"__mixins__",{enumerable:!1,value:[],writeable:!1}).__mixins__.__hidden=!0:r.__mixins__=[]))&&e.call(arguments,function(n,t,e,o){if(o=typeof n=="function",o?(u.prototype=n.prototype,e=new n(r,f)):e=n,e){o&&r.__mixins__.push(n);for(var s in e)s!="constructor"&&s!="prototype"&&(r[s]=e[s])}}),this},Function.define=function(n,r){var f,u=n||function(){},e=arguments.length>1;return typeof n!="function"?(u=n.hasOwnProperty("constructor")?n.constructor:function(){},u.prototype=n,e=!0):(n=r,r=null),e&&t.define.apply(u,arguments),f=function(){Function.initMixins(this),u.apply(this,arguments)},f.prototype=u.prototype,f.constructor=f.prototype.constructor=u.constructor=u.prototype.constructor=f,f},t.defineStatic=function(n){if(n)for(var t in n)this[t]=n[t];return this},f.defineStatic({abstract:!0}),Function.abstract=function(n,t){var r=n||t?function(){typeof n=="function"&&n.apply(this,arguments),typeof t=="function"&&t.apply(this,arguments),typeof n=="string"||f()}.defineStatic({abstract:!0}):f;return r},Function.initMixins=function(t){if(t&&!t.__initMixins__){var r=t,e,o,u,f,s={};for(t.__initMixins__=1;r;)if(r=n?r.__proto__:Object.getPrototypeOf(r),r&&r.hasOwnProperty("__mixins__")&&(e=r.__mixins__)&&(o=e.length))for(u=0;f=e[u],u<o;u++)f in s||(s[f]=1,f.call(t,r,r.constructor));delete t.__initMixins__}},v){o={0:[Function,"initMixins","define","abstract"],1:[t,"fastClass","inheritWith","define","defineStatic"]};for(y in o)for(var s=o[y],p=s[0],h=1,c;c=s[h],h<s.length;h++)a(p,c,{enumerable:!1,value:p[c]})}})();
//# sourceMappingURL=FastClass.min.js.map
</script>

Setup

var InheritPerson = inherit(function () {
                this.init = 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.init = function (name) {
                      base.init(name);
                    };
                
                    this.setAddress = function(city, street) {
                      base.setAddress("France", city, street);
                    };
            });
        
            var InheritParisLover = inherit(InheritFrenchGuy, function(base) {
                
                this.init= function (name) {
                    base.init(name);
                };
                
                this.setAddress = function(street) {
                    base.setAddress("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(name);
                };
            
                this.setAddress = function(city, street) {
                  base.setAddress("France", city, street);
                };
              });
            
              var AugmentParisLover = AugmentFrenchGuy.augment(function(base) {
            
                this.constructor = function (name) {
                  base.constructor(name);
                };
            
                this.setAddress = function(street) {
                  base.setAddress.call(this,"Paris", street);
                  return this;
                };
              });
        
        
        var JRPerson = JRClass.extend({
            init: function(name) {
              this.name = name;
            },
            setAddress: function(country, city, street) {
              this.country = country;
              this.city = city;
              this.street = street;
            }
          });
        
          var JRFrenchGuy = JRPerson.extend({
            init: function(name) {
              this._super(name);
            },
            setAddress: function(city, street) {
              this._super('France', city, street);
            }
          });
        
          var JRParisLover = JRFrenchGuy.extend({
            init: function(name) {
              this._super(name);
            },
            setAddress: function(street) {
              this._super('Paris', street);
              return this;
            }
          });
        
        
        
        
        
        var RaptorPerson = define.Class({
                    init: function(name) {
                        this.name = name;
                    },
                    setAddress: function(country, city, street) {
                        this.country = country;
                        this.city = city;
                        this.street = street;
                    }
                });
                var RaptorFrenchGuy = define.Class({superclass: RaptorPerson}, ['super'], function($super) {
                    var superCtor = $super.constructor;
                    var superSetAddress = $super.setAddress;
            
                    function RaptorFrenchGuy(name) {
                        superCtor.call(this, name);
                    };
            
                    RaptorFrenchGuy.prototype = {
                        setAddress: function(city, street) {
                            superSetAddress.call(this, 'France', city, street);
                        }
                    }
            
                    return RaptorFrenchGuy;
                });
            
                var RaptorParisLover = define.Class({superclass: RaptorFrenchGuy}, ['super'], function($super) {
                    var superCtor = $super.constructor;
                    var superSetAddress = $super.setAddress;
            
                    function RaptorParisLover(name) {
                        superCtor.call(this, name);
                    };
            
                    RaptorParisLover.prototype = {
                        setAddress: function(street) {
                            superSetAddress.call(this, 'Paris', street);
                            return this;
                        }
                    }
            
                    return RaptorParisLover;
                });
        
        
        
        
        var FCPerson = Function.inheritWith(function() {
            return {
                constructor: function(name) {
                        this.nome = name;
                },
                setAddress: function(country, city, street) {
                        this.country = country;
                        this.city = city;
                        this.street = street;
                }
            }
        });
        
        var FCFrenchGuy = FCPerson.inheritWith(function(base, baseCtor){
            return {
                constructor: function(name) {
                  baseCtor.call(this, name);
                },
                setAddress: function(city, street) {
                  base.setAddress('France', city, street);
                }
            }
          });
        
          var FCParisLover = FCFrenchGuy.inheritWith(function(base, baseCtor){
            return {
                constructor: function(name) {
                  baseCtor.call(this, name);
                },
                setAddress: function(street) {
                  base.setAddress('Paris', street);
                  return this;
                }
            }
          });
        
          var dejavuClassPerson2 = dejavu.Class.declare({
            initialize: function(name){
              this.name = name;
            },
            setAddress: function(country, city, street) {
              this.country = country;
              this.city = city;
              this.street = street;
            }
          }, true);
        
          var dejavuClassFrenchGuy2 = dejavu.Class.declare({
            $extends: dejavuClassPerson2,
            initialize: function (name) {
              dejavuClassPerson2.call(this, name);
            },
            setAddress: function(city, street) {
              dejavuClassPerson2.prototype.setAddress.call(this, 'France', city, street);
            }
          }, true);
        
          var dejavuClassParisLover2 = dejavu.Class.declare({
            $extends: dejavuClassFrenchGuy2,
            initialize: function (name) {
              dejavuClassFrenchGuy2.call(this, name);
            },
            setAddress: function(street) {
              dejavuClassFrenchGuy2.prototype.setAddress.call(this, 'Paris', street);
              return this;
            }
          }, true);
        
        var MyPerson = my.Class({
                constructor: function(name) {
                  this.name = name;
                },
                setAddress: function(country, city, street) {
                  this.country = country;
                  this.city = city;
                  this.street = street;
                }
              });
            
              var MyFrenchGuy = my.Class(MyPerson, {
                constructor: function(name) {
                  MyFrenchGuy.Super.call(this, name);
                },
                setAddress: function(city, street) {
                  MyFrenchGuy.Super.prototype.setAddress.call(this, 'France', city, street);
                }
              });
            
              var MyParisLover = my.Class(MyFrenchGuy, {
                constructor: function(name) {
                  MyParisLover.Super.call(this, name);
                },
                setAddress: function(street) {
                  MyParisLover.Super.prototype.setAddress.call(this, 'Paris', street);
                  return this;
                }
              });
        
        var JSFacePerson = jsface.Class({
            constructor: function(name) {
              this.name = name;
            },
            setAddress: function(country, city, street) {
              this.country = country;
              this.city = city;
              this.street = street;
            }
          });
        
          var JSFaceFrenchGuy = jsface.Class(JSFacePerson, {
            constructor: function(name) {
              JSFaceFrenchGuy.$super.call(this, name);
            },
            setAddress: function(city, street) {
              JSFaceFrenchGuy.$superp.setAddress.call(this, 'France', city, street);
            }
          });
        
          var JSFaceParisLover = jsface.Class(JSFaceFrenchGuy, {
            constructor: function(name) {
              JSFaceParisLover.$super.call(this, name);
            },
            setAddress: function(street) {
              JSFaceParisLover.$superp.setAddress.call(this, 'Paris', street);
              return this;
            }
          });
        
        
        var CreatePerson = {
            create: function (name) {
                this.name = name;
                return this.extend();
            },
            setAddress: function(country, city, street) {
                 this.country = country;
                 this.city = city;
                 this.street = street;
            }
        };
        
        var CreateFrenchGuy  = CreatePerson.extend({
            create: function (name) {
                return CreatePerson.create.call(this,name);
            },
            setAddress: function(city, street) {
                CreatePerson.setAddress('France', city, street);
            }
        });
        
        var CreateParisLover  = CreateFrenchGuy.extend({
            create: function (name) {
                return CreateFrenchGuy.create.call(this,name);
            },
            setAddress: function(street) {
                CreateFrenchGuy.setAddress('Paris', street);
                return this;
            }
        });
        
        
        var LineagePerson = Lineage.define("Thing", function(p) {
            p.initialize = initialize;
            function initialize(name) {
                this.name = name;
            }
        
            p.setAddress = setAddress;
            function setAddress(country, city, street) {
                this.country = country;
                this.city = city;
                this.street = street;
            }
        });
        
        var LineageFrenchGuy = Lineage.define("LineageFrenchGuy", LineagePerson, function(p, pp) {
        
            p.initialize = initialize;
            function initialize(name) {
                pp.initialize.call(this, name);
            }
        
            p.setAddress = setAddress;
            function setAddress(city, street) {
                pp.setAddress.call(this,'France', city, street);
            }
        });
        
        var LineageParisLover = Lineage.define("LineageParisLover", LineageFrenchGuy, function(p, pp) {
        
            p.initialize = initialize;
            function initialize(name) {
                pp.initialize.call(this, name);
            }
        
            p.setAddress = setAddress;
            function setAddress(street) {
                pp.setAddress.call('Paris', street);
                return p;
            }
        });
        
        
        var InheritPerson2 = inherit2(function () {
                    this.init = 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.init = function (name) {
                          base.init(name);
                        };
                    
                        this.setAddress = function(city, street) {
                          base.setAddress("France", city, street);
                        };
                });
            
                var InheritParisLover2 = inherit2(InheritFrenchGuy2, function(base) {
                    
                    this.init= function (name) {
                        base.init(name);
                    };
                    
                    this.setAddress = function(street) {
                        base.setAddress("Paris", street);
                        return this;
                    };
                });

Test runner

Ready to run.

Testing in
TestOps/sec
inherit2.js
var t1 = new InheritParisLover2("Mary");
t1.setAddress("CH");
ready
augment.js
var t2 = new AugmentParisLover("Mary");
t2.setAddress("CH");
ready
john resig
var t3 = new JRParisLover("Mary");
t3.setAddress("CH");
ready
raptor
var t4 = new RaptorParisLover("Mary");
t4.setAddress("CH");
ready
FastClass
var t5 = new FCParisLover("Mary");
t5.setAddress("CH");
ready
dejavu optimized
var t6 = new dejavuClassParisLover2("Mary");
t6.setAddress("CH");
ready
my.class
var t7 = new MyParisLover("Mary");
t7.setAddress("CH");
ready
JSFace
var t8 = new JSFaceParisLover("Mary");
t8.setAddress("CH");
ready
True prototype (native)
var t9 = CreateParisLover.create("Mary");
t9.setAddress("CH");
ready
lineage.js
var t10 = new LineageParisLover("Mary");
t10.setAddress("CH");
ready
inherit.js
var t11 = new InheritParisLover("Mary");
t11.setAddress("CH");
ready

Revisions

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