Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const symbols = [];
for (let i = 0; i < 1000; ++i) {
symbols[i] = Symbol(`key_${i}`);
//symbols[i] = `key_${i}`;
}
function test1() {
const map = new Map();
for (let i = 0; i < 10000; ++i) {
map.set(symbols[i], 1);
}
let result = 0;
for (let i = 0; i < 10000; ++i) {
result += map.get(symbols[i]);
}
return result;
}
function test2() {
const object = Object.create(null);
for (let i = 0; i < 10000; ++i) {
object[symbols[i]] = 1;
}
let result = 0;
for (let i = 0; i < 10000; ++i) {
result += object[symbols[i]];
}
return result;
}
let allValues;
let l = 0;
allValues = [];
console.time("Map");
for (let i = 0; i < 1000; i++) {
allValues.push(test1());
}
console.timeEnd("Map");
l += allValues.length;
allValues = [];
console.time("object");
for (let i = 0; i < 1000; i++) {
allValues.push(test2());
}
console.timeEnd("object");
l += allValues.length;
allValues = [];
console.time("object");
for (let i = 0; i < 1000; i++) {
allValues.push(test2());
}
console.timeEnd("object");
l += allValues.length;
allValues = [];
console.time("Map");
for (let i = 0; i < 1000; i++) {
allValues.push(test1());
}
console.timeEnd("Map");
l += allValues.length;
console.log(l);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment