Skip to content

Instantly share code, notes, and snippets.

@sunghwan2789
Forked from jung-kim/map-perf.js
Last active October 15, 2022 00:14
Show Gist options
  • Save sunghwan2789/7fdf0b232f96e1f2dea7fd676f499dce to your computer and use it in GitHub Desktop.
Save sunghwan2789/7fdf0b232f96e1f2dea7fd676f499dce to your computer and use it in GitHub Desktop.
es6 map vs array vs obj performance test
"use strict";
let looper = (callback) => {
let n = 2000000;
while (n > 0) {
callback(n);
n--;
}
};
let timer = (log, callback) => {
let start = Date.now();
callback();
console.log(log, Date.now() - start);
};
let map = new Map();
let obj = {};
let ray = [];
for (let i = 0; i < 2000000; i++) {
ray.push(i);
}
function RandomIndex(count) {
return Math.floor(Math.random() * count);
}
timer("Map int key set took: ", () =>
looper((index) => map.set(RandomIndex(2000000), index))
);
timer("Obj int key set took: ", () =>
looper((index) => (obj[RandomIndex(2000000)] = index))
);
timer("ray int key set took: ", () =>
looper((index) => (ray[RandomIndex(2000000)] = index))
);
console.log();
timer("Map int key get took: ", () =>
looper((index) => {
let dummylet = map.get(RandomIndex(2000000));
})
);
timer("Obj int key get took: ", () =>
looper((index) => {
let dummylet = obj[RandomIndex(2000000)];
})
);
timer("ray int key get took: ", () =>
looper((index) => {
let dummylet = ray[RandomIndex(2000000)];
})
);
console.log();
timer("Map int key iter took: ", () =>
Array.from(map).forEach(([key, val]) => {
let dummylet = val;
})
);
timer("Obj int key iter took: ", () =>
Object.entries(obj).forEach(([key, val]) => {
let dummylet = val;
})
);
timer("Ray int key iter took: ", () =>
ray.forEach((val, key) => {
let dummylet = val;
})
);
console.log("\n\n");
map = new Map();
obj = {};
ray = [];
timer("Map string key set took: ", () =>
looper((index) => map.set("" + RandomIndex(2000000), "" + index))
);
timer("Obj string key set took: ", () =>
looper((index) => (obj["" + RandomIndex(2000000)] = "" + index))
);
timer("ray string key set took: ", () =>
looper((index) => (ray["" + RandomIndex(2000000)] = "" + index))
);
console.log();
timer("Map string key get took: ", () =>
looper((index) => {
let dummylet = map.get("" + RandomIndex(2000000));
})
);
timer("Obj string key get took: ", () =>
looper((index) => {
let dummylet = obj["" + RandomIndex(2000000)];
})
);
timer("ray string key get took: ", () =>
looper((index) => {
let dummylet = ray["" + RandomIndex(2000000)];
})
);
console.log();
timer("Map string key iter took: ", () =>
Array.from(map).forEach(([key, val]) => {
let dummylet = val;
})
);
timer("Obj string key iter took: ", () =>
Object.entries(obj).forEach(([key, val]) => {
let dummylet = val;
})
);
timer("Ray string key iter took: ", () =>
ray.forEach((val, key) => {
let dummylet = val;
})
);
// node --version: v18.11.0
// Map int key set took: 210
// Obj int key set took: 148
// ray int key set took: 25
// Map int key get took: 23
// Obj int key get took: 22
// ray int key get took: 21
// Map int key iter took: 87
// Obj int key iter took: 292
// Ray int key iter took: 10
// Map string key set took: 820
// Obj string key set took: 463
// ray string key set took: 448
// Map string key get took: 35
// Obj string key get took: 84
// ray string key get took: 84
// Map string key iter took: 93
// Obj string key iter took: 521
// Ray string key iter took: 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment