2014-03-03 34 views
0

至少在這個例子中,看起來ECMAScript 6 Map比使用一個對象檢索要慢。在Firefox中,使用下面的代碼:爲什麼Firefox地圖比地圖上的對象慢?

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) 

輸出是:

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 

爲什麼有檢查比對檢查對象慢4倍?

回答

1

在我看來像緩存效果。如果我減少了Map中的項目數量,使它適合我的緩存,我得到了很多更快的查找....

相關問題