fqtyahgzdhklsnx

JavaScript performance comparison

Test case created by

Preparation code

<script src="https://raw.github.com/bestiejs/lodash/master/lodash.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  window.onerror = function (a) {
  console.log(arguments);
  };
  
  function Compiler () {
    this.store = {};
  }
  
  Compiler.prototype.assemble = function(definitions) {
    this.store = _.extend(this.store, definitions);
    return this;
  };
  
  Compiler.prototype.compile = function (callback) {
    _.each(this.store, function (val, id) {
      if (val.$retain === true) {
        val.$value = this.create(id);
      }
    }, this);
    return this;
  };
  
  Compiler.prototype.create = function (id, args) {
    var item = this.store[id] || {};
    if (!item) {
      return id;
    } else if (_.isString(item)) {
      return item;//this.format(item);
    } else if (!item.$instance) {
      return item;
    } else if (item.$value) {
      return item.$value;
    } else if (item.$instance.charAt(0) === "$") {
      return this.create(item.$instance.replace("$", ''), _.extend(item.$params, args));
    } else {
      var object = this.fetch(item.$instance);
      var context = function () {};
      context.prototype = object.prototype;
      var instance = _.extend(new context(), item.$params || {} , args);
      _.each(instance, function (value, key) {
        if (_.isObject(value)) {
          _.each(value, function (svalue, skey) {
            if (skey.charAt(0) === "$") {
              instance[key] = this.create(skey.replace("$", ''), svalue);
            }
          }, this);
        } else if (_.isString(value)) {
          //value = instance[key] = this.format(value);
          if (value.charAt(0) === "$") {
            instance[key] = this.create(value.replace("$", ''));
          }
        } 
      }, this);
      object.call(instance);
      return instance;
    }
  };
  Compiler.prototype.formatter = new RegExp("(%(\w+)%)", "g");
  Compiler.prototype.format = function (string) {
    var self = this;
    return string.replace(this.formatter, 
      function (match, p, specififer) {
        return self.create(specififer);
    });
  };
  
  Compiler.prototype.fetch = function (key) {
    return Object;
    return window[key] || Object;
  }
  /*
  var compiler = new Compiler().assemble({
    "n": "n00n00",
    "parameter": "suck my fucking %n%",
    "factory": {
      "$instance": "View",
      "$params": {
        "name": "omfgeee %parameter%",
      }
    },  
    "singleton": {
      "$instance": "View",
      "$retain": true,
      "$deps": ["abcde", "fghij", "klmnop"],
      "$events": {},
      "$params": {
        "peep": "$parameter",
        "paap": "$factory",
        "poop": {
          "$factory": {
            "name": "leet"
          }
        }
      }
    }
  }).create('singleton', { peeep: "bah"}).render();
  */
  
  window.compiler = new Compiler();
  compiler.assemble({
   "views.form": {
      "$instance": "src/Jinga/core/views/FormView",
       "$params": {
         "template": "@templates.form",
         "name": "lewdksjfcjl",
         "selector": "ewdsfwes",
         "collection": "3ewsr",
         "model": "werrrr",
         "group": "rr"
       }
    }
  });
  
  
  
    function Container () {
      this.store = {};
      this.preLoad = [];
    }
    
    Container.prototype = {
  
      set: function (key, value) {
        this.store[key] = {
          value: value
        };
        return value;
      },
  
      factory: function (key, constructor) {
        return this.store[key] = {
          value: constructor,
          type: '$factory'
        };
        return constructor;
      },
        
      instance: function (key, instance) {
        this.store[key] = {
          value: instance,
          type: '$instance'
        };
        return instance;
      },
      
      preload: function (deps) {
        this.preLoad = this.preLoad.concat(deps);
      },
  
      inject: function (object, params) {
        var cons = function () {};
        cons.prototype = object.prototype;
        var instance = new cons();
        instance.container = this;
        for (var key in params) instance[key] = params[key];
        object.call(instance, params);
        return instance;
      },
  
      get: function (key) {
        var item = this.store[key];
        if(item === undefined) return undefined;
        switch (item.type) {
          case '$instance':
            this.store[key].type = undefined;
            this.store[key].value = item.value(this, arguments[1] || {});
            return item.value;
          case '$factory':
            var object = item.value(this, arguments[1] || {});
            return object;
          default:
            return item.value;
        }
      },
  
      has: function (key) {
        return key in this.store;
      },
        
      compile: function (callback) {
        var self = this;
          for (var key in self.store) {
            var item = self.store[key];
            if (item.type === '$instance') {
              self.store[key].value = item.value(self, {});
              self.store[key].type = undefined;
            } else if (item.type === '$param') {
              self.store[key].value = item.value();
              self.store[key].type = undefined;
            }
          }
          if (callback) callback();
      },
      
      assemble: function (config) {
        for (var key in config) {
          var definition = config[key];
          var plugin = undefined;
          if (key in Container.plugins.assembly) {
            plugin = Container.plugins.assembly[key];
          } else if (definition === Object(definition)) {
            for (var pluginKey in Container.plugins.assembly) {
              if (pluginKey in definition) {
                plugin = Container.plugins.assembly[pluginKey];
                break;
              }
            }
          }
          if (plugin !== undefined) {
            plugin(this, key, definition);
          } else {
            this.set(key, definition);
          }
        }  
      }
    };
    
    Container.plugins = {};
    
    Container.plugins.assembly = {
      "$preload": function (container, key, definition) {
        container.preload(definition);
      },
      "$defaults": function (container, key, definition) {
        container.defaults(definition);
      },
      "$instance": function (container, key, definition) {
        var value = undefined;
        var rval = definition["$instance"];
        delete definition["$instance"];
        compiled = Container.compileDefinition (container, rval, definition);
        eval("value = function (container, args) { return " + compiled + "}");
        container.store[key] = {
          value: value,
          type: '$instance'
        };
      },
      "$factory": function (container, key, definition) {
        var value = undefined;
        var rval = definition["$factory"];
        delete definition["$factory"];
        compiled = Container.compileDefinition (container, rval, definition);
        eval("value = function (container, args) { return " + compiled + "}");
        container.store[key] = {
          value: value,
          type: '$factory'
        };
      }
    };
    
    Container.compileArg = function (container, arg) {
      var type = toString.call(arg);
      if (type == '[object Array]') {
        var compiled = [];
        var argsLen = arg.length;
        for (var i = 0 ; i < argsLen; ++i) {
          compiled.push(Container.compileArg(arg[i]));
        }
        return "[" + compiled.join(",") + "]";
      } else if (type == '[object String]') {
        if (arg.charAt(0) === "@") {
          return 'container.get("' + arg.substr(1) + '")';
        } else if (arg === "&") {
          return 'container';
        } else {
          return '"' + arg + '"';
        }
      } else if (arg === Object(arg)) {
        var compiled = [];
        for (var key in arg) {
          if (key.charAt(0) === "@") {
            return 'container.get("' + key.substr(1) + '", ' + 
              Container.compileArg(container, arg[key]) + ")";
          }
          var kvp = '"' + key + '": ';
          kvp += 'args["' + key + '"] !== undefined ? args["' + key + '"] : ';
          kvp += Container.compileArg(container, arg[key]);
          compiled.push(kvp);
        }
        return "{" + compiled.join(',') + "}";
      } else {
        return arg.toString();
      }
    };
    
    Container.compileDefinition = function (container, object, args) {
      var compiled = '';
      var instanciator = '';
      switch (object.charAt(0)) {
        case '$':
          container.preLoad.push(object.substr(1));
          instanciator = 'container.inject(Object, ';
          break;
        case '@':
          instanciator = 'container.get("' + object.substr(1) + '", ';
          break;
      }
      compiled += instanciator;
      compiled += Container.compileArg(container, args, object) || 'undefined';
      return compiled += ');';
    };
  
  
  window.container = new Container();
  container.assemble({
   "views.form": {
      "$factory": "$src/Jinga/core/views/FormView",
      "template": "@templates.form",
      "name": "lewdksjfcjl",
      "selector": "ewdsfwes",
      "collection": "3ewsr",
      "model": "werrrr",
      "group": "rr"
    }
  });
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
container
window.container;//window.container.get('views.form', { 'template': '@@@@' });
pending…
compiler
window.compiler;
//window.compiler.create('views.form', { 'template': '@@@@' });
pending…

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

Compare results of other browsers

0 Comments