Multi-push vs. multi-map

huangapple go评论59阅读模式
英文:

Multi-push vs. multi-map

问题

alpha 使用 map 函数从数组中获取属性集合,而 beta 通过迭代数组并将每个属性附加到相应的数组中。当数组 a 中的对象包含大量字段(属性)时,哪种方法更快或更慢?

alpha 使用 map 函数可能在处理大量字段时会稍微快一些,因为它允许同时处理所有属性并且可以受益于 JavaScript 引擎的优化。但是,性能差异可能不太明显,除非处理的数据非常庞大。

beta 使用显式迭代,可能会在处理少量属性时更为简单和直观。对于小型数据集,性能差异可能不明显,但当数据规模增大时,alpha 的性能可能会更好一些。

选择哪种方法取决于你的具体需求和代码的可读性。如果你处理的数据量很大,可以考虑使用 map,但要注意在实际应用中进行性能测试,以确定哪种方法更适合你的情况。

英文:

I'm asked to get attributes collection out of an array object,

let a = [
    {name:'aname',age:21},
    {name:'bname',age:22},
    {name:'cname',age:23},
    {name:'dname',age:24},
    {name:'ename',age:25},
    {name:'fname',age:26},
    {name:'gname',age:27}]

// wanted
let ok = {
    names:'aname;bname;cname;dname;ename;fname;gname',
    ages:'21;22;23;24;25;26;27'
}

and I got 2 ways of doing it:

alpha just using map of an array:

// alpha
let res = {
    names:'',
    ages:''
}

res.names=a.map(iter=>iter.name).join(';')
res.ages=a.map(iter=>iter.age).join(';')
//then return res

// ========================================================

and beta just iterate the array and append each attribute in the tabulation array:


// beta
let res = {
    names:[],
    ages:[]
}

a.forEach(iter=>{
    res.names.push(iter.name)
    res.ages.push(iter.age)
})
// then handle res's fields
ok.names = res.names.join(';')
ok.ages = res.ages.join(';')

so which way should I use to get the collection? Will alpha get slower or faster than beta when the objects in a get lots of fields(attrs)?

答案1

得分: 1

两种方法都不错。我会说这取决于你的个人偏好,你想使用哪一种。

然而,从性能的角度来看,我觉得以下方法会产生更好的结果。

let a = [
    {name:'aname',age:21},
    {name:'bname',age:22},
    {name:'cname',age:23},
    {name:'dname',age:24},
    {name:'ename',age:25},
    {name:'fname',age:26},
    {name:'gname',age:27}]
    
let ok = { names: '', ages: ''}

for (let i = 0; i < a.length; i++){
const iter = a[i]
ok.names += iter.name + ";";
ok.ages += iter.age + ";";
}

ok.names = ok.names.slice(0,-1)
ok.ages = ok.ages.slice(0,-1)

console.log(ok)

这种方法消除了创建新数组或连接它们的需要(连接是一种重操作)。只需创建你想要的字符串,在最后移除多余的分号即可。

英文:

Both approaches are good. I'd say it depends on your personal preference what you'd want to use.

However, It seems to me that if you are aiming for performance, the following would yield better results.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let a = [
    {name:&#39;aname&#39;,age:21},
    {name:&#39;bname&#39;,age:22},
    {name:&#39;cname&#39;,age:23},
    {name:&#39;dname&#39;,age:24},
    {name:&#39;ename&#39;,age:25},
    {name:&#39;fname&#39;,age:26},
    {name:&#39;gname&#39;,age:27}]
    
let ok = { names: &#39;&#39;, ages: &#39;&#39;}

for (let i = 0; i &lt; a.length; i++){
const iter = a[i]
ok.names += iter.name + &quot;;&quot;;
ok.ages += iter.age + &quot;;&quot;;
}

ok.names = ok.names.slice(0,-1)
ok.ages = ok.ages.slice(0,-1)

console.log(ok)

<!-- end snippet -->

This apporach eliminates the need to create new arrays or joining them (join is a heavy operation). Just create the string you want and at the end of it all, remove the one extra semicolon.

答案2

得分: 0

我认为“alfa”对我来说更简单、更清晰,但我猜这取决于你...

英文:

I consider that alfa is simpler and clearer for me, but I guess it is up to you...

huangapple
  • 本文由 发表于 2023年2月16日 17:04:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定