re: Stop Telling People For Loops Are Bad VIEW POST

re: Thanks for sharing your view. I have to say I disagree on your basic premise, though. The whole purpose of methods such as map, filter and redu...

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)

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) {

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.


So you're saying you have a nuanced and reasonable approach to a twitter-hot-take-bait topic? That's crazy-talk! 😉

Code of Conduct Report abuse