Skip to content
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

no-array-for-each Slower Performance #1522

Closed
luniv04 opened this issue Sep 13, 2021 · 3 comments
Closed

no-array-for-each Slower Performance #1522

luniv04 opened this issue Sep 13, 2021 · 3 comments
Labels

Comments

@luniv04
Copy link

luniv04 commented Sep 13, 2021

I went to https://jsbench.me/ and put in the following two tests...

Test 1:

var x = new Set([1, 2, 3, 4, 5, 6, 7, 8]);
x.forEach(value => {
	console.log(value);
});

Test 2:

var x = new Set([1, 2, 3, 4, 5, 6, 7, 8]);
for(const p of x) {	
	console.log(p);
}

The rule "no-array-for-each" is not better to use. The documentation indicates that using "for of" has better performance, but I dont believe it does. I may have better performance for straight arrays, but when it comes to Maps and Sets, I am finding better performance with using the built in "forEach()" method.

@luniv04 luniv04 added the bug label Sep 13, 2021
@fisker
Copy link
Collaborator

fisker commented Sep 13, 2021

You can disable it, if you prefer .forEach().

@fisker fisker closed this as completed Sep 13, 2021
@fisker fisker reopened this Sep 13, 2021
@fisker
Copy link
Collaborator

fisker commented Sep 13, 2021

Sorry, accidentally closed it, I believe this is mistake made by #1149, this rule not mean to improve performance, but only readability.

@sindresorhus
Copy link
Owner

Micro-benchmarking code snippets like this is completely meaningless and does not reflect real-world performance. A "for-of" loop is faster, as it doesn't need to execute the body function for each iteration (it's just a scope). This especially matter if the loop body is complex.

Repository owner locked as resolved and limited conversation to collaborators Nov 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants