为什么forEach比常规迭代器更受青睐? [英] Why should forEach be preferred over regular iterators?

查看:91
本文介绍了为什么forEach比常规迭代器更受青睐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 airbnb javascript指南.有一个特别的声明,内容为:

I was reading airbnb javascript guide. There is a particular statement, that says:

不要使用迭代器.优先使用JavaScript的高阶函数,而不要使用for-in或for-of.这样的循环.

Don’t use iterators. Prefer JavaScript’s higher-order functions instead of loops like for-in or for-of.

他们提供上述声明的原因是:

The reason they give for the above statement is:

这将强制执行我们的不变规则.处理带有返回值的纯函数比副作用更容易推论.

This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.

我无法区分给出的两种编码做法:

I could not differentiate between the two coding practices given:

    const numbers = [1, 2, 3, 4, 5];

    // bad
    let sum = 0;
    for (let num of numbers) {
     sum += num;
    }
    sum === 15;

    // good
    let sum = 0;
    numbers.forEach((num) => {
     sum += num;
    });

    sum === 15;

谁能解释,为什么为什么forEach比常规的for循环更受青睐?真正有什么不同?使用常规iterators有什么副作用吗?

Could anyone explain, why should forEach be preferred over regular for loop? How does it really make a difference? Are there any side effects of using the regular iterators?

推荐答案

Airbnb样式指南中的这种推理适用于用于不变性的数组方法,例如filtermapreduce等,但是不是forEach:

This reasoning in Airbnb style guide applies to array methods that are used for immutability, which are filter, map, reduce, etc. but not forEach:

这将强制执行我们的不变规则.处理带有返回值的纯函数比副作用更容易推论.

This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.

所以比较更像是:

// bad
let sum = 0;
for (let num of numbers) {
 sum += num;
}
sum === 15;

// bad
let sum = 0;
numbers.forEach((num) => {
 sum += num;
});

sum === 15;

// good
const sum = numbers.reduce((num, sum) => sum += num, 0);

sum === 15;

通常,for > forEach > for..of > for..in在性能方面.在几乎所有引擎中,这种关系都是统一的,但是可能因不同的数组长度而有所不同.

Generally, for > forEach > for..of > for..in in terms of performance. This relationship is uniform in almost all engines but may vary for different array lengths.

forEach是在最新的Chrome/V8中进行了重大改进(基于 this 综合测试):

forEach is the one that was significantly improved in latest Chrome/V8 (almost twice, based on this synthetic test):

由于它们都非常快,因此,除非有其他证明,否则仅出于性能原因而选择不太合适的循环方法就可以视为初步优化.

Since all of them are fast, choosing less appropriate loop method just because of performance reasons can be considered preliminary optimization, unless proven otherwise.

for..of相比,forEach的主要优点在于,即使在ES3中,前者也可以进行多填充,并且提供值和索引,而后者更易读,但应在ES5及更低版本中进行移植.

The main benefits of forEach in comparison with for..of is that the former be polyfilled even in ES3 and provides both value and index, while the latter is more readable but should be transpiled in ES5 and lower.

forEach具有已知的陷阱,使其在某些情况下不适合使用forfor..of正确处理:

forEach has known pitfalls that make it unsuitable in some situations that can be properly handled with for or for..of:

  • 回调函数创建新上下文(可以使用箭头函数寻址)

  • callback function creates new context (can be addressed with arrow function)

不支持迭代器

不支持生成器yieldasync..await

没有提供使用break

这篇关于为什么forEach比常规迭代器更受青睐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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