我刚学习了MapReduce,所以我想知道是否有编写相关程序的优势。
const initialValue = 0;
if (this.items) {
return this.items.filter(function (item) {
return item && item.quantity && item.price;
}).reduce(function(previousValue, currentValue) {
return previousValue + currentValue.quantity * currentValue.price ;
}, initialValue);
} else {
return initialValue;
}
不仅仅是
let total = 0;
if (this.items) {
this.items.forEach(function(item) {
if (item && item.quantity && item.price) {
total += item.quantity * item.price;
}
});
}
return total;
forEach
没有被设计或实现为并行运行,则它不适用于分布式环境,第二种解决方案将无法用于并行计算,因此这两种解决方案完全不同。 - keenthinker