Skip to content

Instantly share code, notes, and snippets.

@paberr
Created April 8, 2018 16:12
Show Gist options
  • Save paberr/1d916343631c0e42f8311a6f2782f30d to your computer and use it in GitHub Desktop.
Save paberr/1d916343631c0e42f8311a6f2782f30d to your computer and use it in GitHub Desktop.
Large ECMAScript Maps compared to Object
/*
* 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');
@paberr
Copy link
Author

paberr commented Apr 8, 2018

Results on NodeJS:

  • Map:
push: 11.795ms
remove: 9.051ms
both same: 7704.086ms
both different: 55.570ms
  • Object:
push: 4.001ms
remove: 6.726ms
both same: 7.195ms
both different: 7.479ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment