简体   繁体   English

这些ArrayToMap函数用法中的哪一种在性能方面更好?

[英]Which one of these ArrayToMap functions usages is better performance-wise?

Trying to Map an Array, which one of these implementations is better performance-wise? 尝试映射数组,以下哪种实现在性能上更好? Is there a better solution? 有更好的解决方案吗?

//Given the following Array of people:
const people = [ { name: 'Alice', available: true },  { name: 'Bob', available: false }, { name: 'Charlie', available: true }];

const mapWithReduce = (people) => people.reduce((map, person) => ({ [person.name]: person.available, ...map }), {});

const mapWithForEach = (people) => {
    const map = {};
    people.forEach((person) => map[person.name] = person.available);
    return map;
}

I find mapWithReduce prettier but I don't know if ...map} is copying the map every iteration. 我发现mapWithReduce更漂亮,但我不知道... map}是否在每次迭代中都复制地图。 mapWithForEach seems more performant. mapWithForEach似乎更有效。

I like this solution. 我喜欢这个解决方案。

 const people = [ { name: 'Alice', available: true }, { name: 'Bob', available: false }, { name: 'Charlie', available: true } ]; const peopleMap = people.reduce((map, person) => { map[person.name] = person; return map; }, {}); console.log(peopleMap); 

Looks equals than the forEach solution but without creating local vars. 看起来比forEach解决方案相等,但没有创建本地var。

https://jsperf.com/arraymapeachreduce/9 https://jsperf.com/arraymapeachreduce/9

在此处输入图片说明 在此处输入图片说明

Performance-wise, using a for loop is the fastest. 在性能方面,使用for循环最快。

基准

 const people = [{ name: 'Alice', available: true }, { name: 'Bob', available: false }, { name: 'Charlie', available: true }] const mapWithForLoop = (key, value) => array => { const map = {} for (let i = 0; i < array.length; i++) { const entry = array[i] map[entry[key]] = entry[value] } return map } const mapPeopleWithForLoop = mapWithForLoop('name', 'available') console.log(mapPeopleWithForLoop(people)) 

The forEach() method comes close, though. 但是, forEach()方法接近。

they should both be identical if you make their implementations the same and reduce is a little easier to grok because developers know that it has an accumulator whereas in the forEach, you're just sort of implementing reduce yourself 如果您使它们的实现相同,并且应该简化reduce,它们应该都相同,因为开发人员知道它有一个累加器,而在forEach中,您只是在实现reduce自己

const mapWithReduce = (people) => people.reduce((map, person) => 
  {
    map[person.name]: = person.available;
    return map
  }, {}
);

edit: it's possible that under the hood forEach is more performant, but if you're using babel or another transpiler, it's probably a moot point because at this point, they should do the work of making the more idiomatic version performant. 编辑:有可能forEach的性能更高,但是如果您使用的是babel或其他编译器,那么这可能是一个有争议的问题,因为在这一点上,他们应该努力使版本更加惯用。 ( source ) 来源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在性能方面,在算法复杂度方面,以下两种用于将字符串的第一个字母首字母化的JS功能哪个更好,为什么? - Performance-wise,algorithmic complexity-wise which of the following two JS fucntion for capitializing the first letter of a string is better and why? 在这种情况下,缓存jQuery对象是否会带来更好的性能结果? - Does caching a jQuery object in this case give better results performance-wise? 有没有比 crypto.randomBytes 更好的方法来在性能方面生成唯一的 id? - Is there a better way than crypto.randomBytes to generate unique ids in performance-wise? jquery,性能方面什么是更快的getElementById或jquery选择器? - jquery, performance-wise what is faster getElementById or jquery selector? 性能方面 - 画布 vs 基本 URI vs 图像 - Performance-wise - canvas vs base URI vs image Javascript DOM与XSLT转换在性能方面如何? - Javascript DOM vs. XSLT Transformation performance-wise? 是否更好地使用.delegate()性能? - Is it better to use .delegate() performance wise? 在访问数组长度方面,哪一种性能更好 - Which one is better in performance in accessing length of array 在这种特定情况下,if-else 是否比 try-catch 更好,性能方面? 哪种方式是最佳实践? - Is if-else better than try-catch in this particular scenario, performance wise? Which way is the best practice? if + override变量vs.if + else条件。 性能上哪个更好? - if+override variable vs. if+else condition. Which is better, performance wise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM