英文:
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:'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)
<!-- 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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论