参见英文答案 > es6 Map and Set complexity, v8 implementation????????????????????????????????????1个
在幕后,在V8中,JavaScript-Map-object的键是以某种方式索引的,它优化了map.get方法?或者map.get()只是循环遍历整个地图,直到它发现一个关键匹配?
在幕后,在V8中,JavaScript-Map-object的键是以某种方式索引的,它优化了map.get方法?或者map.get()只是循环遍历整个地图,直到它发现一个关键匹配?
我对map.get在500,000个键/值对的较大映射上的效率感兴趣.我有这么多映射,我只想缓存在RAM中,而不是查询已经为快速值检索索引密钥的数据库.在我看来,如果Map对象的键以某种方式在幕后索引,那么查询RAM而不是数据库会更快.
抽象:
function randomUniqueThing()
{
// returns (magically) a unique random:
// string, number, function, array or object.
}
var objMap = new Map();
var count = 0;
var thing1,thing2;
while(count < 500000)
{
thing1 = randomUniqueThing();
thing2 = randomUniqueThing();
objMap.set(thing1, thing2);
count++;
}
var lastValue = objMap.get(thing1); // Will getting this last
// thing's value take longer
// than getting other values?