Last active
March 11, 2020 20:20
-
-
Save areinmeyer/8d9d8d00d6d47abd77d5a0fb36cfc3b1 to your computer and use it in GitHub Desktop.
Javascript Maps Insertion/Deletion speed versus Objects.
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
/** | |
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