reduce vs map + fromEntries vs reduce spread
Date tested:
one year ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0
Test name
Executions per second
reduce
1385.3 Ops/sec
map + fromEntries
1028.6 Ops/sec
reduce spread
0.3 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) };
Tests:
reduce
Object.entries(data).reduce((acc, [k, v]) => { acc[k] = v.toString(); return acc; }, {});
map + fromEntries
Object.fromEntries(Object.entries(data).map(([k, v]) => ([k, v.toString()])));
reduce spread
Object.entries(data).reduce((acc, [k, v]) => ({ ...acc, [k]: v.toString(), }), {});
Open this result on MeasureThat.net