r/javascript • • Dec 16 '18

Coming from Python to JS NSFW

const print = console.log

I found my safe space.

607 Upvotes

156 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Dec 16 '18

[deleted]

16

u/Earhacker Dec 17 '18

for loops are for dirty Python casuals. Over here in JavaScript we use Array.prototype.forEach.

(but sure, we use your fornum loop sometimes too)

0

u/linuxdropout Dec 22 '18

If you want your code to be approximately 90% slower -_-

1

u/linuxdropout Dec 22 '18

1

u/Earhacker Dec 23 '18

If you really need those milliseconds, forEach is not your only problem. Performance isn't everything.

1

u/linuxdropout Dec 23 '18

It's not a matter of milliseconds it's that it's an entire magnitude slower. Sure if you're just doing a single foreach loop then it is what it is. But if you're doing a "common pattern" of filter -> map I've seen all over code bases. And then you do that in 10 different places on a front-end application, all at page load time. All of a sudden you've added an entire second to time-to-interactive.

I get the whole maintainability thing. And there are plenty of places that it ends up being much cleaner and easier to maintain using map/filter/reduce/foreach. And in some places you can add functionality with callback chains or async-await.

But it's something to be aware of that I've seen many more developers just use Array functions as the "default" without thinking.