Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript Maps Insertion/Deletion speed versus Objects.
/**
Goal is a simple (naive?) test to show performance differences in Maps vs Objects.
**/
//Insert entries into Map
const start = Date.now()
let testmap = new Map()
for (let i = 0; i < 1000000; i++) {
testmap.set(i, i)
}
const end = Date.now()
console.log(`Map Insert Timer is ${end - start}`)
//Delete entries into Map
const startMapD = Date.now()
for (let i = 0; i < 1000000; i++) {
testmap.delete(i, i)
}
const endMapD = Date.now()
console.log(`Map Delete Timer is ${endMapD - startMapD}`)
//Insert entries into an object
const start2 = Date.now()
let testobj = {}
for (let i = 0; i < 1000000; i++) {
testobj[i.toString()] = i
}
const end2 = Date.now()
console.log(`Obj Insert Timer is ${end2 - start2}`)
//Delete entries in an Object
const startD = Date.now()
for (let i = 0; i < 1000000; i++) {
delete testobj[i.toString]
}
const endD = Date.now()
console.log(`Obj Delete Timer is ${endD - startD}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.