jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
what's the fastest way to convert an arguments instance to an array? Also, certain patterns prevent V8 from optimizing the complete function in nodejs as pointed out here. only accessing arguments via index or passing to apply are valid methods.
// shortcuts for fairness
// ----------------------
var arrayPush = Array.prototype.push,
arrayConcat = Array.prototype.concat,
objectToString = Object.prototype.toString;
(function(){
Object.defineProperty(arguments.constructor.prototype, 'slice', {
value: Array.prototype.slice
});
}());
// competitors
// -----------
function pushApply(){
var args = []; // js is lisp
return arrayPush.apply(args, arguments), args;
}
function loopAssign(){
for(var i = arguments.length, args = new Array(i); i--;) args[i] = arguments[i];
return args;
}
function loopPush(){
for(var i = arguments.length, args = []; i--;) args.push(arguments[i]);
return args;
}
function loopPushShort(){
var len = arguments.length,
i = 0,
args = [];
while (args.push(arguments[i++]) !== len) {}
return args;
}
function arrayOf(){
return (Array.of || loopPush).apply(null, arguments);
}
function arrayApply(){
return Array.apply(null, arguments);
}
function argumentsSlice(){
return arguments.slice();
}
// test function
// -------------
function test(fn){
var i, len = i = 100,
args = new Array(len),
result;
while(i--) args[i] = Math.random() * i;
result = fn.apply(null, args);
if (objectToString.call(result) !== '[object Array]' ||
result.length !== len
){
throw new Error(fn.name + ' failed the test');
}
}
Ready to run.
Test | Ops/sec | |
---|---|---|
pushApply |
| ready |
loopAssign |
| ready |
loopPush |
| ready |
arrayOf |
| ready |
arrayApply (limited) |
| ready |
argumentsSlice |
| ready |
loopPushShort |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.