对于 Array,使用 map() & 是否更有效?在javascript中减少()而不是forEach

2023-03-15前端开发问题
8

本文介绍了对于 Array,使用 map() & 是否更有效?在javascript中减少()而不是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 线程可以执行.所以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 函数.这是关于它周围的框架;即使使用糟糕的map"和reduce"功能,它也会为您提供良好的性能(除非您在 shuffle 步骤中失去对数据集大小的控制!).

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).

仅仅因为它有一个map"和一个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中减少()而不是forEach()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13