DEV Community

Discussion on: Array.map() much slower than for loop

Collapse
 
jessev6 profile image
Jesse Verbruggen

This actually is a lot slower since mapping an array will create a copy of each value in order to not modify the original array.

Since a for loop does not copy the values but rather just accesses them using their index, it is a lot faster.

This peformance difference is only noticable when you have a large amount of data. For smaller amounts of data, it's better to write code which feels more natural to you, which may explain why map is used so commonly.

Collapse
 
henryjw profile image
Henry Williams

Thanks for the perspective. I'm going to try out the same test with creating a copy of each value in the for loop