Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created December 24, 2016 11:44
Show Gist options
  • Save OliverJAsh/794fee1ce273fa6e6bbb7d1373845d02 to your computer and use it in GitHub Desktop.
Save OliverJAsh/794fee1ce273fa6e6bbb7d1373845d02 to your computer and use it in GitHub Desktop.
Mapping a Map: spread vs manual performance
(() => {
const IterableH = {
map: fn => iterable => [...iterable].map(fn),
};
const MapH = {
map: fn => map => new Map(IterableH.map(([ key, value ]) => [ key, fn(value) ])(map)),
map2: fn => map => {
const newMap = new Map()
for ([ key, value ] of map) {
newMap.set(key, fn(value))
}
return newMap;
},
}
const map = new Map(_.range(0, 1000).map(x => [ x, x ]))
console.time('map');
MapH.map(value => value + 1)(map);
console.timeEnd('map');
console.time('map2');
MapH.map2(value => value + 1)(map);
console.timeEnd('map2');
})()
@OliverJAsh
Copy link
Author

Testing in Chrome 57 beta:

map: 1.68ms
map2: 0.983ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment