Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Object.fromEntries vs reduce vs. for-of
Turning an array into an object
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser:
Chrome 122
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Object.fromEntries
14913.4 Ops/sec
Reduce (reuse object)
29408.5 Ops/sec
Reduce (creating temporary objects)
1982.1 Ops/sec
For-of
27737.5 Ops/sec
Script Preparation code:
function rand() { return Math.floor(Math.random() * 10000) } var data = Array(1000).fill(0).map((_, i) => ({ i, a: rand(), b: rand() }))
Tests:
Object.fromEntries
Object.fromEntries(data.map(({i, ...rest}) => [i, rest]));
Reduce (reuse object)
data.reduce((acc, {i, ...rest}) => { acc[i] = rest; return acc; }, {});
Reduce (creating temporary objects)
Object.entries(data).reduce((acc, {i, ...rest}) => ({ ...acc, [i]: rest }), {});
For-of
const result = {} for (const {i, ...rest} of data) { result[i] = rest; }