Question

At least in this example, it seems the ECMAScript 6 Map is slower to retrieve than using an object. In Firefox, using the following code:

map = {};
var i=1000000;
console.time('populate');
while (i--) {
    map[i] = 'value of '+i;
}
console.timeEnd('populate');

console.time('has');
var i=2000000;
var ctr = 0;
while (i--) {
    if (map.hasOwnProperty(i)) {
        ctr++;
    }
}
console.timeEnd('has');
console.log(ctr)





map = new Map();
var i=1000000;
console.time('populate');
while (i--) {
    map.set(i, 'value of '+i);
}
console.timeEnd('populate');

console.time('has');
var i=2000000;
var ctr = 0;
while (i--) {
    if (map.has(i)) {
        ctr++;
    }
}
console.timeEnd('has');
console.log(ctr)



map = {};
var i=1000000;
console.time('populate');
while (i--) {
    map[i] = 'value of '+i;
}
console.timeEnd('populate');

console.time('has');
var i=2000000;
var ctr = 0;
while (i--) {
    if (map.hasOwnProperty(i)) {
        ctr++;
    }
}
console.timeEnd('has');
console.log(ctr)

The output is:

populate: timer started Maptest:16
populate: 465.51ms Maptest:20
has: timer started Maptest:22
has: 133.03ms Maptest:30
1000000 Maptest:31
populate: timer started Maptest:39
populate: 418.26ms Maptest:43
has: timer started Maptest:45
has: 414.44ms Maptest:53
1000000 Maptest:54
populate: timer started Maptest:60
populate: 347.55ms Maptest:64
has: timer started Maptest:66
has: 124.67ms Maptest:74
1000000

Why would has check be 4x slower than for the object check?

No correct solution

OTHER TIPS

Looks to me like cache effects. If I reduce the number of items in the Map such that it fits in my cache, I get much faster lookups....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top