Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Last active December 16, 2015 12:49
Show Gist options
  • Save AutoSponge/5437038 to your computer and use it in GitHub Desktop.
Save AutoSponge/5437038 to your computer and use it in GitHub Desktop.
var strategy = {};
strategy["slice"] = function () {
return Array.prototype.slice.call(arguments, 0);
};
strategy["cache slice"] = (function () {
var slice = Array.prototype.slice;
return function () {
return slice.call(arguments, 0);
};
}());
strategy["bound slice"] = (function () {
var slice = Function.call.bind(Array.prototype.slice);
return function () {
return slice(arguments, 0);
};
}());
strategy["for"] = function () {
for (var args = [], i = 0, len = arguments.length; i < len; i += 1) {
args[i] = arguments[i];
}
return args;
};
strategy["push"] = function () {
var args = [];
Array.prototype.push.apply(args, arguments);
return args;
};
strategy["cache push"] = (function () {
var push = Array.prototype.push;
return function () {
var args = [];
push.apply(args, arguments);
return args;
};
}());
strategy["bound push"] = (function () {
var push = Function.apply.bind(Array.prototype.push);
return function () {
var args = [];
push(args, arguments);
return args;
};
}());
strategy["concat"] = function () {
return Array.prototype.concat.apply([], arguments)
};
strategy["cache concat"] = (function () {
var concat = Array.prototype.concat;
return function () {
return concat.apply([], arguments);
};
}());
strategy["bound concat"] = (function () {
var concat = Function.apply.bind(Array.prototype.concat);
return function () {
return concat([], arguments);
};
}());
//http://jsperf.com/arguments-slice1
var strategy = {};
strategy["Array call"] = function () { //winner
return Array.call.apply(Array, arguments);
};
strategy["slice"] = function () {
return Array.prototype.slice.call(arguments, 1);
};
strategy["cache slice"] = (function () {
var slice = Array.prototype.slice;
return function () {
return slice.call(arguments, 1);
};
}());
strategy["bound slice"] = (function () {
var slice = Function.call.bind(Array.prototype.slice);
return function () {
return slice(arguments, 1);
};
}());
strategy["for"] = function () {
for (var args = [], i = 1, len = arguments.length; i < len; i += 1) {
args[i - 1] = arguments[i];
}
return args;
};
strategy["while"] = function () {
var args = [], i = arguments.length;
while (i > 0) {
args[i - 2] = arguments[--i];
}
return args;
};
strategy["reduce"] = function () {
return Array.prototype.reduce.call(arguments, function (args, e, i) {
return i === 0 ? args : (args[args.length] = e, args);
}, []);
};
strategy["filter"] = function () {
return Array.prototype.filter.call(arguments, function (e, i) {
if (i > 0) {
return e;
}
});
};
//test Array.bind.apply(Array, [1,2,3,4,5])()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment