-
-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use for of instead of forEach where possible #1845
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I like forEach more:
- it's easier to cast type of item being iterated
- easier access to item index and update of array being iterated
- I frequently confuse for-in and for-of (having hard time figuring what iterates over what)
.forEach is great for the scenarios that you described. This PR only changes the cases where none of these conditions are met. Also |
By using for of I found to instances of not handleded promises. Tagging the previous author according to git blame
sortedBlocks.forEach((block) => this.chain.receiveBlock(block, false)); Should it be awaited or not?
validators.forEach((v) => v.start()); |
We don't really care about awaiting there. If we are gonna await, Promise.all should be better fit. That said, it has nothing to do with for-of vs forEach. It's just that in forEach callback we didn't put async keyword ( |
The error |
Code Climate has analyzed commit 5597e5c and detected 6 issues on this pull request. Here's the issue category breakdown:
View more on Code Climate. |
add brackets in forEach :) |
Sure, but here we should compare the one-line syntax to be fair. validators.forEach(v => v.start()) for (const v of validators) v.start() The error will show up in the second and not in the first. In multiline, it will show up in both styles |
That's not equivalent. for (const v of validators) {
(function() { return return v.start() })()
} |
Why would you want that in a for loop? |
You don't. You don't wan't that in forEach which is what we/I did there since void-ing promise wasn't working in previous eslint version when you didn't want to await promise. |
no we don't have to but it should also be ok to use await here too |
Would you mind taking a look yourself @tuyennhv? That function uses non straightforward Promise pattern that I don't understand |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you see if there is a noticeable performance cost to using for-of instead of forEach?
This is a very popular question and different benchmarks show different results, where sometimes forEach wins others for ... of. I guess it depends on the platform or engine then? If performance is important somewhere (like SSZ bytes comparison), we should use the regular for loop which is faster and noticeable once you do very large numbers of iterations (+1e6) |
I think the difference between the two methods would be insignificant compared to the async operation happening inside the loop, probably not the most important place to optimize! |
Agree, the reasons for choosing between for of and forEach should be code readability, features and subjective preference |
for of instead of forEach: