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
And so we see, wrapping it into function that uses the simplest naive approach inside produces the fastest results (at least for now). Why? Probably because this func can be easily JIT-ted
let arr = [];
for (i = 0; i < 1000; i++) {
arr[i] = {key: i, value: i+1}
}
function objarr2obj(ar, {key, value}) {
const redfunc = (obj, item) => (obj[item[key]] = item[value], obj)
return ar.reduce(redfunc, {})
}
function objarr2obj_naive(ar, {key, value}) {
var res = {}; // maybe it'll be better not to imply constant-ness every time
for (const obj of arr) {
res[obj[key]] = obj[value]
}
return res
}
function objarr2obj_naive_const(ar, {key, value}) {
const res = {}; // No it's not. const da king
for (const obj of arr) {
res[obj[key]] = obj[value]
}
return res
}
Ready to run.
Test | Ops/sec | |
---|---|---|
using Array.reduce |
| ready |
using Object.fromEntries |
| ready |
using Array.reduce shortened |
| ready |
using Array.reduce shortened (wrapped into func) |
| ready |
using unpacking for |
| ready |
using unpacking for (wrapped into func) |
| ready |
using unpacking for <const> (wrapped into func) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.