案例:list1长度是4133,list2长度37208;
foreach循环
console.time();
list1.forEach((el,index,arr) => {
el.lists =new Array();
list2.forEach((it) => {
if (el.sgc == it.sgc) {
el.lists.push(it);
}
});
});
console.timeEnd();
// default: 4569.388916015625 ms
map循环
console.time();
list1.map((el,index,arr) => {
el.lists =new Array();
list2.map((it) => {
if (el.sgc == it.sgc) {
el.lists.push(it);
}
});
});
console.timeEnd();
// 6319.599853515625 ms
循环次数为4133*37208;循环速度forEach比map快1,750.2109375ms