Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

epidemian

1
Posts
A member registered May 08, 2019

Recent community posts

(2 edits)

It's worth noting that, although the numbers on Node.js are quite bad for forEach() and reduce(), on browsers the story is quite different. Chrome yields pretty similar numbers for the four snippets, with reduce() even being slightly faster than all the alternatives:

for: 1850.9541015625ms
for of: 1702.959228515625ms
forEach: 1835.01708984375ms
reduce: 1259.451904296875ms

And Firefox yields impressively better results, having reduce() also at the top:

for: 137ms - timer ended
for of: 150ms - timer ended
forEach: 103ms - timer ended
reduce: 54ms - timer ended

So, as it usually happens with these sort of things, it's not a matter of "forEach() being slow". It's a matter of different JS engines applying different optimizations. In general, i think it's better to stick to the most idiomatic or clearer alternative. Not only does that make the code more readable, but there's also a high chance that those idiomatic patterns will be optimized by JS engines in the (not so distant) future.

Only on cases where we have actually measured a performance impact on the target runtimes we want to run, it'd be adviceable to go for the more "performant" but less idiomatic/readable solution. And even in those cases, i'd recommend leaving a comment explaining why we went for a less idiomatic solution, just in case JS engines catch up and it stops being the case that that is the most performant option :)