js 循环多次和循环一次的时间的性能对比 All In One
const creative_boxes = [];
this.ruleForm.creativeBoxList.forEach(item => {
if(!item.isDynamic) {
// creative_ids_map 需要 for 循环两次 (代码行数少,循环次数多)
const creativeIds = creative_ids_map.map(obj => obj.creative_id) ?? [];
const creativeIdsMap = creative_ids_map.map(obj => ({
creative_id: obj.creative_id,
creative_name: obj. creative_name,
})) ?? [];
creative_boxes.push({
creative_ids: creativeIds,
creative_ids_map: creativeIdsMap,
});
}
});
const creative_boxes = [];
this.ruleForm.creativeBoxList.forEach(item => {
if(!item.isDynamic) {
const creativeIds = [];
const creativeIdsMap = [];
// creative_ids_map 需要 for 循环一次 (代码行数多,循环次数少)
for (const obj of creative_ids_map) {
const {
creative_id,
creative_name,
} = obj;
creativeIds.push(creative_id);
creativeIdsMap.push({
creative_id,
creative_name,
});
}
creative_boxes.push({
creative_ids: creativeIds,
creative_ids_map: creativeIdsMap,
});
}
});
console.time & console.timeEnd
console.time("answer time");
console.timeLog("answer time");
console.timeEnd("answer time");
js no for loop & 100 Array
const arr = [...``.padStart(100, ` `)].map((item, i) => ({id: i + 1,}));
https://www.cnblogs.com/xgqfrms/p/8982974.html
refs
https://developer.mozilla.org/en-US/docs/Web/API/console#timers
https://developer.mozilla.org/en-US/docs/Web/API/console/timeEnd
https://developer.mozilla.org/en-US/docs/Web/API/console/time
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!