DEV Community

Discussion on: Javascript - forEach vs map method in Arrays

disgustingdev profile image

Just a quick note here:
forEach is mutable - means he operates with the same piece of memory, allocated for defined array, but giving some CPU resources for restructuring this piece after some changes in loop

map is immutable - means it just allocates new place in memory for your results, where RAM is giving storaging resources and CPU does much less

So there is always a choice if you need to calculate slower but to save some memory, or vice-versa