Ad
  • Default User Avatar

    Just did a benchmark myself, looping over 10.000.000 array-entries:

    for-of: 154.071ms
    for-in: 143.209ms
    for-each: 153.508ms
    for: 11.204ms
    

    From what i read, the forEach was drastically slower in previous node-versions. But since year or so at least, it is now on an equal level. But seems that, if you want performance, you should use the standard for-loop, because it's just way faster.
    But i also understand your opinion on the pure function part.

  • Default User Avatar

    I wouldn't agree with their opinion. To me dealing with pure functions that return values is easier. The advantages of using custom for loops over forEach however is that you can use arbitrary control structures (return, break, continue, yield, await) in the loop body.

  • Default User Avatar

    Same solution as mine, but to add the missing 1%:

    • You can add to the directionValues the possible keys n, s, w, e.
    • Add return type to main function and the function you pass in the forEach.

    There are several people out there who say that forEach is bad, but i didn't verify any of those.