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
Fisher-Yates Array Shuffle variants: reduce vs forEach vs for
function rand(max = 1, min = 0) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function shuffleArrayReduce(array) {
return array.reduceRight(
(acc, _, i) => {
const j = rand(i);
[acc[i], acc[j]] = [acc[j], acc[i]];
return acc;
},
[...array]
);
}
const shuffleReduceNoSpread = arr => arr.reduceRight((r,_,__,s) =>
(r.push(s.splice(0|Math.random()*s.length,1)[0]), r),[])
function shuffleArrayFor(array) {
const a = [...array];
for (i = array.length - 1; i > 0 ;i--) {
const j = rand(i);
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function shuffleArrayForEach(array) {
const a = [...array];
array.forEach((_, i) => {
const j = rand(i);
[a[i], a[j]] = [a[j], a[i]];
})
return a;
}
function shuffleArraySort(array) {
return array.sort( ()=>Math.random()-0.5 );
}
const arr = [...Array(256)].map(() => rand(1024))
Ready to run.
Test | Ops/sec | |
---|---|---|
reduceRight |
| ready |
for |
| ready |
forEach |
| ready |
shuffleArraySort(arr) |
| ready |
shuffleReduceNoSpread |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.