Test name | Executions per second |
---|---|
Object.fromEntries | 2715.7 Ops/sec |
Reduce (reuse object) | 18107.6 Ops/sec |
Reduce (creating temporary objects) | 121.1 Ops/sec |
Map | 2077.8 Ops/sec |
forEach | 18834.5 Ops/sec |
for of | 18485.0 Ops/sec |
for i | 1461.3 Ops/sec |
for i saved length variable | 2625.2 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;
}