Anyway I wouldn't be concerned about the speed of forEach vs map.
A lot of people would be!
And therein lies the problem.
Programmers working with JavaScript sometimes seem like they are running the particle colliders at CERN.
When one image on your page is bigger than jQuery, and you are worried about having to "bring in all of jQuery when we only use half of it", you need to take a deep breath and start again.
Who does have to worry about this? The Google's and Facebook's of the world, where a change that is a few bytes more expensive can cost millions of dollars.
So I understand the logical reasoning behind not doing mutations with a functional.method with an emphasis on immutability but in this case I don't see much if a problem would you care to elaborate what's wrong with it?
The intent is no longer clear. .map has a very clear intent - give me a new array with some transform performed to all the elements. Don't mutate the original array.
It communicates conflicting intentions. The map says "any side effects are a mistake and should be put in the bug tracker" while the loop body says "I'm nothing but a side effect." That kind of thing makes the code harder to read for the next person two years down the line.
No particular reason other than the fact that I can be a little cynical and part of me believes the community will opt for map simply because it's shorter and "cooler" to use functional programming techniques. The fact that there is nothing in Javascript (that I know of anyway) to enforce referential transparency means that use of the FP techniques will likely be little more than lip service to the concepts.
I'm pretty hard on the Scala and Haskell communities when it comes to being able to make FP practical and accessible for everyday use, but at least they understand how to get the benefits of FP. I currently do not have any such confidence in the Js community.
I'm not sure, I might quite possibly have misinterpreted (apologies if that's the case), but just in case, the motivation behind not using map and reduces to perform mutations isn't out of an idealistic "functional programming is good" or "mutations are bad" dogma. It's for the benefit of making your code easier to understand for yourself and others
Specifically, it's an implicit promise that the code inside the function, no matter how complex, can be safely ignored as long as the value it returns is as expected. There is huge value in being able to rely on the (albeit implicit) referential transparency for maps and reduces in the amount of code you can skip when you're combing for bugs or trying to understand a section of a program someone else wrote.
And I can tell you of one way of enforcing referential transparency - code review :)
I've run into a few javascript programmers who've made the same mistake with misusing map (2 juniors and one self-taught rockstar), but after I've explained it the responses were always immediately "that makes sense". That gives me hope that while it may be an easy mistake to make, it won't get touted as a best practice because people will understand why they shouldn't be doing it as soon as you explain it to them, and hopefully pass it forward :)
use the most succing code then take a quick look at the profile against a large set of data.
I would say, always ... write the most READABLE code that you still feel is performant, then profile it. If it's fine from an overall performance standpoint, you are good.
That's it! It's not rocket science (well, it might be, but usually not if we're talking about things like JavaScript).
You don't even need to comment it! It just makes sense from looking at it and is fine. Who cares if it's 3 ms slower than the other version that is 10 lines longer? If that is not impacting your users, move on.
0
u/[deleted] Jun 10 '16
[deleted]