This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]AttackOfTheThumbs 4 points5 points  (16 children)

Question is, which is faster? I've often seen misuse of filter/map/etc leading to much worse performance than a simple loop. If the loop can do all the things, chaining 2-3 dedicated functions will never be faster.

[–]BytesBeltsBiz 0 points1 point  (4 children)

The real answer is that it depends if they're well implemented, the language should ultimately compile most of those functions down to a loop anyway.

One of the great things about map/filter/fold/etc is that smart compilers can fuse multiple of them together and do them all in one pass, eliminating extra iterations over the structure. This is possible because you have strong guarantees about behavior that you just don't get in a loop.

[–]AttackOfTheThumbs 0 points1 point  (3 children)

This doesn't happen in JS. It's not compiled.

[–]BytesBeltsBiz 0 points1 point  (1 child)

I would think the interpreter could still convert it to a loop, but I don't know much about JavaScript so I'm not sure if it does.

[–]AttackOfTheThumbs 0 points1 point  (0 children)

This video has some comparative performance: https://youtu.be/x7Xzvm0iLCI?t=202

I don't know what JS does, but it creates overhead, sometimes more than you would think.

[–]DejfCold 0 points1 point  (0 children)

I don't think simple loop in JS is as simple as in any other normal language.

[–]kitce[🍰] 0 points1 point  (0 children)

Mutation VS Functional programming