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
const smallKOverN = 0.01
const largeKOverN = 0.1
const k = 100
const largeArr = [...Array(Math.floor(k/smallKOverN )).keys()]
const smallArr = [...Array(Math.floor(k/largeKOverN )).keys()]
const swrs = {
bestOfVerbose: (arr, k) => {
if (k/arr.length < 0.02) { return swrs.indexOf(arr, k); }
return swrs.shuffle(arr, k);
},
bestOfShort: (arr, k) => bestSwr[k/arr.length < 0.02](arr, k),
indexOf: (arr, k) => {
const idxs= [];
const n = arr.length;
while(idxs.length < k) {
const r = Math.floor(Math.random() * n);
if(idxs.indexOf(r) === -1) {
idxs.push(r);
}
}
return idxs.map(i => arr[i]);
},
shuffle: (arr, k) => {
arr = arr.slice();
const n = arr.length;
for (let i=0; i<k; ++i) {
const index = Math.floor(Math.random() * (n - i)) + i;
const t = arr[index];
arr[i] = arr[index];
arr[index] = t;
}
return arr.slice(0, k);
},
shuffleEcma: (arr, k) => {
arr = arr.slice();
const n = arr.length;
for (let i=0; i<k; ++i) {
const index = Math.floor(Math.random() * (n - i)) + i;
[arr[i], arr[index]] = [arr[index], arr[i]]
}
return arr.slice(0, k);
},
shuffleRev: (arr, k) => {
arr = arr.slice()
const n = arr.length
for (let i=n-1; i>n-k; --i) {
const index = Math.floor(Math.random() * (i + 1));
const t = arr[index]
arr[i] = arr[index]
arr[index] = t
}
return arr.slice(n-k);
},
shuffleEcmaRev: (arr, k) => {
arr = arr.slice();
const n = arr.length;
for (let i=n-1; i>n-k; --i) {
const index = Math.floor(Math.random() * (i + 1));
[arr[index], arr[i]] = [arr[i], arr[index]]
}
return arr.slice(n-k);
},
takenMapArr: (arr, k) => {
const taken = []
const result = []
const n = arr.length;
while (result.length < k) {
const i = Math.floor(Math.random() * n);
if (!taken[i]) {
taken[i] = true;
result.push(arr[i]);
}
}
return result
},
takenMapObj: (arr, k) => {
const taken = {}
const n = arr.length;
const result = []
while (result.length < k) {
const i = Math.floor(Math.random() * n);
if (!taken[i]) {
taken[i] = true;
result.push(arr[i]);
}
}
return result
}
}
const bestSwr = {true: swrs.indexOf, false: swrs.shuffleRev}
Ready to run.
Test | Ops/sec | |
---|---|---|
bestOfVerbose (small k/n) |
| ready |
bestOfShort (small k/n) |
| ready |
shuffle (small k/n) |
| ready |
shuffleRev (small k/n) |
| ready |
shuffleEcma (small k/n) |
| ready |
shuffleEcmaRev (small k/n) |
| ready |
takenMapObj (small k/n) |
| ready |
takenMapArr (small k/n) |
| ready |
indexOf (small k/n) |
| ready |
bestOfVerbose (large k/n) |
| ready |
bestOfShort (large k/n) |
| ready |
shuffle (large k/n) |
| ready |
shuffleRev (large k/n) |
| ready |
shuffleEcma (large k/n) |
| ready |
shuffleEcmaRev (large k/n) |
| ready |
takenMapObj (large k/n) |
| ready |
takenMapArr (large k/n) |
| ready |
indexOf (large k/n) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.