Skip to content

Instantly share code, notes, and snippets.

@aburd
Created October 12, 2019 16:22
Show Gist options
  • Save aburd/31c84f0a3db5e392e182c59d02c5ea1b to your computer and use it in GitHub Desktop.
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)
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