Skip to content

Instantly share code, notes, and snippets.

@CodeMan99
Created August 29, 2017 04:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeMan99/18d590d12aef1e90d2ed262f4fab0c96 to your computer and use it in GitHub Desktop.
Save CodeMan99/18d590d12aef1e90d2ed262f4fab0c96 to your computer and use it in GitHub Desktop.
function arrayFrom(a) {
return Array.from(arguments).slice(1);
}
function arraySlice(a) {
return Array.prototype.slice.call(arguments, 1);
}
function argsRest(a, ...args) {
return args;
}
function argsSpread(a) {
return [...arguments].slice(1);
}
function forArgs(a) {
var len = arguments.length;
var args = new Array(len - 1);
for (var i = 0; i < len; ++i) {
args.push(arguments[i]);
}
return args;
}
var i;
i = 0;
console.time('Array.from');
while (i++ < 1000000) {
arrayFrom(i, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k');
}
console.timeEnd('Array.from');
i = 0;
console.time('Array.prototype.slice.call');
while (i++ < 1000000) {
arraySlice(i, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k');
}
console.timeEnd('Array.prototype.slice.call');
i = 0;
console.time('...args rest');
while (i++ < 1000000) {
argsRest(i, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k');
}
console.timeEnd('...args rest');
i = 0;
console.time('...args spread');
while (i++ < 1000000) {
argsSpread(i, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k');
}
console.timeEnd('...args spread');
i = 0;
console.time('for (;;)');
while (i++ < 1000000) {
forArgs(i, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k');
}
console.timeEnd('for (;;)');
@CodeMan99
Copy link
Author

Output:

Array.from: 3369.938ms
Array.prototype.slice.call: 4687.895ms
...args rest: 25.267ms
...args spread: 1135.578ms
for (;;): 89.194ms

@vfor4
Copy link

vfor4 commented Dec 27, 2021

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment