Created
December 20, 2023 16:11
-
-
Save egm0121/3ff7c0b83cbe07097032c94049428636 to your computer and use it in GitHub Desktop.
Performance Comparison of reduce+spread vs reduce+Object.Assign
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arrOfMaps = new Array(40000).fill(0).map((k,i) => { | |
return { [i]:`testdsfsdfdsfsdfsdfsdf${i}`}; | |
}) | |
console.time('functional') | |
const finalMap = arrOfMaps.reduce((acc, current) => ({ ...acc, ...current }), {}); | |
console.log('finalMap keys', Object.keys(finalMap).length); | |
console.timeEnd('functional') | |
console.time('object assign') | |
const finalMapAssign = arrOfMaps.reduce((acc, current) => { | |
return Object.assign(acc, current); | |
}, {}); | |
console.log('object assign keys', Object.keys(finalMapAssign).length); | |
console.timeEnd('object assign') // this will be 150X faster because no intermediate objects are created after each iteration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment