News for hackers. Stuff that matters.
Re:Show HN: Canvas engines performance comparison – PixiJS, ... by hombre_fatal on Tuesday May 05, 2020 @07:39PM (#23086367)
I can agree with your point in general, but [...Array(N).keys()].forEach() is not the most readable way to write "do this N times".

It creates an array of length N, but for obscure-to-most-people reasons, Array(N).forEach() doesn't work, so they Rube Goldberged their way to an array that they could call forEach() on. Their solution was to use Array#keys to get an iterable from the array. But an iterable doesn't have a .forEach() method, so they iterate the iterable into another array just to iterate it again with Array#forEach. Frankly the only thing this seems optimized for is to solve the problem without the for-loop for some reason.

The for-loop, on the other hand, is an instantly obvious solution. It's how programmers have been expressing "do this N times" for decades across languages.

Re:Show HN: Canvas engines performance comparison – PixiJS, ... by akiselev on Tuesday May 05, 2020 @09:09PM (#23086941)
Especially when Array(N).fill(0).forEach((_, key) => ...) gives you the same exact functionality without the second eager array if you're seriously trying to avoid for loops.
Re:Show HN: Canvas engines performance comparison – PixiJS, ... by hombre_fatal on Tuesday May 05, 2020 @10:42PM (#23087457)

    Array.from({ length: N }).forEach()
Re:Show HN: Canvas engines performance comparison – PixiJS, ... by Aeolun on Tuesday May 05, 2020 @11:48PM (#23087742)

  for(var i = 0; i < N; i++) {}
Re:Show HN: Canvas engines performance comparison – PixiJS, ... by chii on Wednesday May 06, 2020 @12:24AM (#23087890)
This is the best and cleanest way.

Unless there's a reason to use Array.forEach such as automatic paralellization or some other SIMD-like optimization that can be done that cannot be done in a forloop.

A lot of cargo-cult programming seem to take functional programming to mean programming using the map/reduce/foreach functions, and you end up with shitty code like [...Array(N).keys()].foreach(), just so you can somehow claim that you're doing functional programming.

Re:Show HN: Canvas engines performance comparison – PixiJS, ... by realharo on Wednesday May 06, 2020 @07:11AM (#23089699)
`let` instead of `var`. No need to leak the variable into the outer scope.
Re:Show HN: Canvas engines performance comparison – PixiJS, ... by hombre_fatal on Wednesday May 06, 2020 @01:42AM (#23088196)
That was already proposed upstream.

The person above wanted a way to actually create an array of length N that could be used with .forEach().