map vs spread (v2)

Revision 2 of this benchmark created on


Setup


function dictSet(
  dict,
  key,
  value,
) {
  const newMap = new Map(dict);
  newMap.set(key, value);
  return newMap;
};


const arr = Array.from({ length: 1000 }, (_, i) => i);


const obj = arr.reduce((acc, val) => ({...acc, [String(val)]: { id: val, name: val + 'name' } }), {})

const map = new Map(Object.entries(obj))
console.log(obj, map);


Test runner

Ready to run.

Testing in
TestOps/sec
map
const toto = dictSet(map, '1001', { id: 1001, name: 1001 + 'name' })
ready
spread
const toto = {...obj, [1001]: {id: 1001, name: 1001 + 'name'}}
ready
assign
const toto = Object.assign(obj, { [1001]:   { id: 1001, name: 1001 + 'name' } } )
ready

Revisions

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