This is a great response. You make valid points about these functions communicating intent. I don't think there is anything wrong with these functions because I use them quite a lot myself. It's only when I am dealing with large arrays of data (which is not all too often) that I will choose a for loop.
I agree a for loop where the index is defined in the loop and incremented looks horrendous. I use for..of in combination with Array.entries() these days if I need a loop and the index value.
Your example could be cleaned up considerably using a for..of loop and doing the following:
const betterThings = [];
for (const thing of things) {
const newThing = betterThing(thing)
betterThings.push(newThing)
}
You could even go one step further and not bother with the newThing constant and just do this:
const betterThings = [];
for (const thing of things) {
betterThings.push(betterThing(thing));
}
I am not using Array.entries() here because we don't need the index, we just want the value inside of the array. You can't argue that map does not look cleaner and in this instance, I would also use map as well. If things was comprised of 50,000 things, I might think twice, but a few hundred or even thousand, I would stick with map.
I definitely agree though, as I said at the bottom of the article. Write your code now and optimise it later on if it becomes a problem. Chances are you're only going to incur 100 or so milliseconds using map or any other method, to the point where you or anyone else wouldn't even be able to tell.
Coding is as much a matter of personal growth as it is of logic and control-flow. I keep patience, curiosity, & exuberance in the same toolbox as vim and git.
*Opinions posted are my own*
This is a great response. You make valid points about these functions communicating intent. I don't think there is anything wrong with these functions because I use them quite a lot myself. It's only when I am dealing with large arrays of data (which is not all too often) that I will choose a for loop.
I agree a for loop where the index is defined in the loop and incremented looks horrendous. I use
for..of
in combination withArray.entries()
these days if I need a loop and the index value.Your example could be cleaned up considerably using a
for..of
loop and doing the following:You could even go one step further and not bother with the
newThing
constant and just do this:I am not using
Array.entries()
here because we don't need the index, we just want the value inside of the array. You can't argue thatmap
does not look cleaner and in this instance, I would also usemap
as well. Ifthings
was comprised of 50,000 things, I might think twice, but a few hundred or even thousand, I would stick withmap
.I definitely agree though, as I said at the bottom of the article. Write your code now and optimise it later on if it becomes a problem. Chances are you're only going to incur 100 or so milliseconds using
map
or any other method, to the point where you or anyone else wouldn't even be able to tell.So you're saying you have a nuanced and reasonable approach to a twitter-hot-take-bait topic? That's crazy-talk! 😉