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.
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.