Test name | Executions per second |
---|---|
Object.fromEntries | 2252.3 Ops/sec |
Reduce (reuse object) | 11298.3 Ops/sec |
Reduce (creating temporary objects) | 113.5 Ops/sec |
Map | 1913.4 Ops/sec |
forEach | 11441.5 Ops/sec |
for of | 12784.1 Ops/sec |
for i | 1182.4 Ops/sec |
for i saved length variable | 2181.9 Ops/sec |
var data = Array.from(Array(10000).keys()).map(i => ({id: i, value: `val_${i}`}));
Object.fromEntries(data.map(obj => [obj.id, obj]));
data.reduce((acc, obj) => {
acc[obj.id] = obj;
return acc;
}, {});
data.reduce((acc, obj) => ({
acc,
[obj.id]: obj
}), {});
new Map(data.map(obj => [obj.id, obj]));
const res = {};
data.forEach(obj => {
res[obj.id] = obj;
});
const res = {};
for (const obj of data) {
res[obj.id] = obj;
}
const res = {};
for (let i = 0; i < data.length; i++) {
const obj = data[i];
res[obj.id] = obj;
}
const res = {};
const length = data.length;
for (let i = 0; i < length; i++) {
const obj = data[i];
res[obj.id] = obj;
}