Created
April 8, 2018 16:12
-
-
Save paberr/1d916343631c0e42f8311a6f2782f30d to your computer and use it in GitHub Desktop.
Large ECMAScript Maps compared to Object
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
/* | |
* Map | |
*/ | |
let map = new Map(); | |
console.time('push'); | |
for (let i = 0; i < 100000; i++) { | |
map.set(i, i); | |
} | |
console.timeEnd('push'); | |
console.time('remove'); | |
for (let i = 0; i < 100000; i++) { | |
map.delete(i); | |
} | |
console.timeEnd('remove'); | |
for (let i = 0; i < 100000; i++) { | |
map.set(i, i); | |
} | |
console.time('both same'); | |
for (let i = 0; i < 100000; i++) { | |
map.delete(0); | |
map.set(0, 0); | |
} | |
console.timeEnd('both same'); | |
console.time('both different'); | |
for (let i = 0; i < 100000; i++) { | |
map.delete(i % 1000); | |
map.set(i % 1000, i % 1000); | |
} | |
console.timeEnd('both different'); | |
/* | |
* Object | |
*/ | |
map = Object.create(null); | |
console.time('push'); | |
for (let i = 0; i < 100000; i++) { | |
map[i] = i; | |
} | |
console.timeEnd('push'); | |
console.time('remove'); | |
for (let i = 0; i < 100000; i++) { | |
delete map[i]; | |
} | |
console.timeEnd('remove'); | |
for (let i = 0; i < 100000; i++) { | |
map[i] = i; | |
} | |
console.time('both same'); | |
for (let i = 0; i < 100000; i++) { | |
delete map[0]; | |
map[0] = 0; | |
} | |
console.timeEnd('both same'); | |
console.time('both different'); | |
for (let i = 0; i < 100000; i++) { | |
delete map[i % 1000]; | |
map[i % 1000] = i % 1000; | |
} | |
console.timeEnd('both different'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on NodeJS: