reduce vs forEach for a Map

Benchmark created on


Setup

const items = Array.from({length: 400}, () => `${ Math.floor(Math.random() * 400)}`);

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
const ret = new Map();
items.forEach((item, index) => ret.set(item, index));
return ret;
ready
reduce
return items.reduce((acc, item, index) => { acc.set(item, index); return acc; }, new Map())
ready
map
return new Map(items.map((item, index) => [item, index]))
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.