简体   繁体   English

for.in和.forEach()之间是否存在性能差异?

[英]Is there a performance difference between for…in and .forEach()?

I was curious if anybody knows if one is faster than the other in either specific use cases or in general, and why. 我很好奇,是否有人知道在特定用例中还是在一般情况下,一个人比另一个人快,为什么?

Specifically, I'm referring to 具体来说,我指的是

for (let myVariable in myArray) { *do something* }

and

myArray.forEach(myVariable => { *do something* })

I found an article to back up my opinion that for loops must be faster since they don't have a callback to deal with Wich also is my answer. 我找到了一篇文章来支持我的观点,即for循环必须更快,因为它们没有回调来处理Wich,这也是我的答案。

https://hackernoon.com/javascript-performance-test-for-vs-for-each-vs-map-reduce-filter-find-32c1113f19d7 https://hackernoon.com/javascript-performance-test-for-vs-for-each-vs-map-reduce-filter-find-32c1113f19d7

myArray.forEach(myVariable => { *do something* })

If you use this it'll optimize your code and speed. 如果您使用它,它将优化您的代码和速度。 As forEach loop won't create external indexing like for loop. 至于forEach循环将不会像for循环那样创建外部索引。 More of above it will allow you to access it's different elements easily. 上面有更多内容,您可以轻松访问它的不同元素。 For further use cases, you may visit www.javascriptinfo.com 有关更多用例,请访问www.javascriptinfo.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM