Created
October 12, 2019 16:22
-
-
Save aburd/31c84f0a3db5e392e182c59d02c5ea1b to your computer and use it in GitHub Desktop.
Comparison of getting and setting speeds of Object vs. Map in Javascript (Map writes are very slow)
This file contains 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 testKeyName = 'foo' | |
const testAmount = 100000000 | |
const obj = { [testKeyName]: 'bar' } | |
const map = new Map() | |
map.set(testKeyName, 'bar') | |
let objTestName = `Reading a property from obj ${testAmount} times` | |
console.time(objTestName) | |
for (let i = 0; i < testAmount; i++) { | |
obj[testKeyName] | |
} | |
console.timeEnd(objTestName) | |
let mapTestName = `Reading a property from map ${testAmount} times` | |
console.time(mapTestName) | |
for (let i = 0; i < testAmount; i++) { | |
map.get(testKeyName) | |
} | |
console.timeEnd(mapTestName) | |
objTestName = `Writing a property to obj ${testAmount} times` | |
console.time(objTestName) | |
for (let i = 0; i < testAmount; i++) { | |
obj[testKeyName] = i | |
} | |
console.timeEnd(objTestName) | |
mapTestName = `Writing a property to map ${testAmount} times` | |
console.time(mapTestName) | |
for (let i = 0; i < testAmount; i++) { | |
map.set(testKeyName, i) | |
} | |
console.timeEnd(mapTestName) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment