对于Array,使用map()&更高效。在JavaScript中使用reduce()而不是forEach()? [英] For Array, is it more efficient to use map() & reduce() instead of forEach() in javascript?

查看:87
本文介绍了对于Array,使用map()&更高效。在JavaScript中使用reduce()而不是forEach()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我们知道,map()和reduce()没有副作用。如今,我们手机上也有多核心。那么使用它们会更有效吗?

1)As we know, there's no side-effect with map() and reduce(). Nowadays, we also have muti-core on cell phone. So is it more efficient to use them?

2)另一方面,js只有1个线程可以在大多数浏览器上执行。那么map()和reduce()是为服务器端脚本编写的吗?

2)On the other hand, there's only 1 thread for js to execute on most of the browsers. Therefor map() and reduce() are prepared for server-side scripting?

推荐答案

很容易被忽视,但关键是获得MapReduce的好处是

It is easily overlooked, but the key to getting the benefits of MapReduce is to

A)利用优化的随机播放。通常,您的map和reduce函数可以用慢速语言实现,只要shuffle(最昂贵的操作)得到很好的优化,它仍然可以快速和可扩展。

A) Exploit the optimized shuffle. Often, your map and reduce functions can be implemented in a slow language, as long as the shuffle - the most expensive operation - is well optimized, it will still be fast and scalable.

B)利用检查点功能从节点故障中恢复(但希望你的CPU内核不会失败)。

B) Exploit the checkpointing functionality to recover from node failures (but hopefully, your CPU cores won't fail).

所以最后,map-reduce实际上既不是map,也不是reduce函数。这是围绕它的框架;即使使用错误的地图和缩小功能,这也会给你带来良好的性能(除非你在随机播放步骤中失去对数据集大小的控制!)。

So in the end, map-reduce is actually neither about the map, nor the reduce functions. It's about the framework around it; which will give you good performance even with bad "map" and "reduce" functions (unless you lose control over your data set size in the shuffle step!).

通过在单个节点上执行多线程map-reduce获得的收益相当低,并且很可能有更好的方法来实现共享内存架构的并行化,而不是map-reduce 。 。

The gains to be obtained from doing a multi-threaded map-reduce on a single node are fairly low, and most likely there are much better ways of parallelizing your shop for shared memory architectures than map-reduce...

不幸的是,这些天围绕mapreduce有很多炒作(而且理解力太少)。如果您查找原始论文,它会详细介绍备份任务 ,机器故障和局部优化(对于内存中的单主机用例都没有意义)。

Unfortunately, there is a lot of hype (and too little understanding) surrounding mapreduce these days. If you look up the original paper, it goes into detail about "Backup Tasks", "Machine Failures" and "locality optimization" (neither of which makes sense for an in-memory single-host use case).

仅仅因为它有一个地图 和reduce并没有使它成为mapreduce。

如果它有优化的随机播放,节点崩溃和落后者恢复,那么它只是一个MapReduce。

Just because it has a "map" and a "reduce" doesn't make it a "mapreduce" yet.
It's only a MapReduce if it has an optimized shuffle, node crash and straggler recovery.

这篇关于对于Array,使用map()&更高效。在JavaScript中使用reduce()而不是forEach()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆