简体   繁体   中英

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.

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. More of above it will allow you to access it's different elements easily. For further use cases, you may visit www.javascriptinfo.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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