Class - Native vs Hot

Benchmark created on


Preparation HTML

<script>
  /*
        {
                name    : "hot.core",
                author  : "Artem Smirnov",
                version : 0.01,
                provide : {
                        hot : {
                                typeof
                                clone
                                implement
                                type
                                extend
                        }
                }
        }
  */
  
  (function (Object) {
        
        var toString, hot, types, typeOf, clone, type, implement, extend; // Define variables
        
        toString = Object.prototype.toString;  // Define function to string
        
        hot = this.hot = function (val) {
                return new hot[typeOf(val)](val);
        }; // Hight level but slow function, i will change role of this function
        
        typeOf = hot.typeOf = function (val) {
                //Try to define type using typeof
                var type = typeof val;
                if (typeof val !== "object") {
                        return type;
                }
                
                //[Object type]
                type = toString.call(val).charCodeAt(8);
                
                if (type in typeOf.types) return typeOf.types[type];
                
                //Argument?
                if (val.calee) {
                        return "arguments";
                }
                
                return "object";
        };//Very fast typeOf
        
        typeOf.types = [];
        ['Array',  'Date', 'Number', 'String', 'Boolean', 'Function', 'RegExp', 'Class'].forEach( function (name, i) {
                typeOf.types[name.charCodeAt(0)] = name.toLowerCase();
        });
        
        clone = hot.clone = function (obj) {
                if (obj === null || typeof(obj) != "object") {
                        return obj;
                }
                
                var copy = {};
                
                for (var key in obj) {
                        copy[key] = clone(obj[key]);
                }
                
                return copy;
        }; // Return clone of object
        
        extend    = hot.extend = function (child, parent) {
                for (var key in parent) {
                        child[key] = parent[key];
                }
        }; // Add props from parent to child
        
        type = hot.type = function (type) {
                
                if (type.Init) {
                        var Constructor = type.Init;
                } else {
                        var Constructor = function () {};
                }
                
                for (var prop in type) {
                        if ( type.hasOwnProperty(prop) ) {
                                if ("Init,Extend,Implement,Static".indexOf(prop) === -1) {
                                        Constructor.prototype[prop] = type[prop];
                                }
                        }
                }
                
                if (type.Static) {
                        var Static = type.Static;
                        for (var prop in Static) {
                                if ( Static.hasOwnProperty(prop) ) {
                                        Constructor[prop] = Static[prop];
                                }
                        }
                }
                
                if (type.Extend) {
                        var parent = type.Extend;
                        Constructor.prototype.parent = parent;
                        
                        for (var prop in parent) {
                                if ( typeof Constructor[prop] == "undefined" ) {
                                        Constructor[prop] = parent[prop];
                                }
                        }
                        
                        for (var prop in parent.prototype) {
                                if ( typeof Constructor.prototype[prop] == "undefined" ) {
                                        Constructor.prototype[prop] = parent.prototype[prop];
                                        console.log("yaa");
                                }
                        }
                        
                }
                
                if (type.Implement) {
                        var Implements = type.Implement;
                        
                        Implements.forEach(function (mixin) {
                                for (var prop in mixin) {
                                        if (typeof Constructor.prototype[prop] == "undefined") {
                                                Constructor.prototype[prop] = mixin[prop];
                                                console.log(Constructor.prototype[prop]);
                                        }
                                }
                        });
                        
                }
                
                //Define constructor prop
                Constructor.prototype.constructor = Constructor;
                //Define superclass prop
                Constructor.superclass = extend;
                
                return Constructor;
        };
        
        implement = hot.implement = function (child, parent) {
                extend(child.prototype, clone(parent.prototype));
        };
        
        return hot;
        
  })(Object);
  
  __native = function (x) {
      this.x = x;
  };
  __native.prototype = {
       get_x : function () {
          return this.x;
       } 
  }
  
  __hot = hot.type({
        Init : function (x) {
                this.x = x;
        },
        get_x : function () {
                return this.x;
        }
  });
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Native
var foo = new __native(4);
var bar = foo.get_x();
ready
Hot
var foo = new __hot(4);
var bar = foo.get_x();
ready

Revisions

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