admin管理员组文章数量:1327262
Consider the following two snippets (from this jsperf entry):
let val = 0;
for(let i of indices) {
val += map.get(i);
}
// ---
let val = 0;
for(let i of indices) {
val += obj[i];
}
Here, map
is a Map
, obj
is a plain old JavaScript object (let obj = {}
), and indices
is an array of random indices. Both obj
and map
have been pre-populated with data so the lookups actually return data. Check out the jsperf for the full code.
Question:
Why does the plain old javascript object out-perform the Map
by a factor of 5+? Is this simply because as of writing, Map
s are still very new and un-optimised? Or is there some overhead in Map
lookups that will always keep it from being as fast as a POJO?
If it's just not optimised yet, can we expect it to be faster than a POJO for random lookups eventually? Why? Why not?
Consider the following two snippets (from this jsperf entry):
let val = 0;
for(let i of indices) {
val += map.get(i);
}
// ---
let val = 0;
for(let i of indices) {
val += obj[i];
}
Here, map
is a Map
, obj
is a plain old JavaScript object (let obj = {}
), and indices
is an array of random indices. Both obj
and map
have been pre-populated with data so the lookups actually return data. Check out the jsperf for the full code.
Question:
Why does the plain old javascript object out-perform the Map
by a factor of 5+? Is this simply because as of writing, Map
s are still very new and un-optimised? Or is there some overhead in Map
lookups that will always keep it from being as fast as a POJO?
If it's just not optimised yet, can we expect it to be faster than a POJO for random lookups eventually? Why? Why not?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 2, 2017 at 5:19 user993683user993683 4- 1 I suspect it's because of the method call overhead. Also, you are abusing the collection(s) as an array, storing only integer indices. For which property access is super-optimised. Try with some type of key. – Bergi Commented Jun 2, 2017 at 5:23
-
@Bergi fixed the array (though to be clear, it's not relevant to this question) cheers
本文标签: ecmascript 6JavaScript Map much slower than Object for random lookupsStack Overflow
版权声明:本文标题:ecmascript 6 - JavaScript Map much slower than Object for random look-ups? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742212082a2433896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论