Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Created October 27, 2011 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AutoSponge/1318517 to your computer and use it in GitHub Desktop.
Save AutoSponge/1318517 to your computer and use it in GitHub Desktop.
Arguments Speed Boost
//don't slice arguments just to iterate
//http://jsperf.com/handle-args
(function () {
var i, len;
for (i = 0, len = arguments.length; i < len; i += 1) {
console.log(arguments[i], i);
}
}(3, 2, 1));
//don't prepend arguments
//http://jsperf.com/prepend-args
var list = [3];
(function () {
var i, len, llen;
for (i = 0, llen = list.length, len = llen + arguments.length; i < len; i += 1) {
console.log(llen > i ? list[i] : arguments[i - llen], i);
}
}(2, 1));
list = [3];
//don't slice args for "bindStrict"
bindStrict = function (fn, obj) {
var args= [], i, len = arguments.length;
for (i = 2; i < len; i += 1) {
args[i - 2] = arguments[i];
}
return function () {
return fn.apply(obj, args);
};
};
//don't shift arguments (after making it an array), use a function return value instead
//http://jsperf.com/composites
var Component = function(config) {
this.config = config || {};
};
Component.prototype.set = function (key, value) {
this.config[key] = value;
};
Component.prototype.get = function (key) {
return this.config[key];
};
var Composite = function (config) {
var component = new Component(config),
cache = {};
this.delegate = function (action) {
return cache[action] ? cache[action] : cache[action] = function (args) {
return component[action].apply(component, args);
};
};
};
Composite.prototype.set = function (key, val) {
this.delegate("set")(arguments);
};
Composite.prototype.get = function (key) {
return this.delegate("get")(arguments);
};
var c = new Composite;
c.set("test", 1);
c.get("test");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment