DEV Community

Discussion on: Practical Functional Programming in JavaScript - Why it's worth it

Collapse
 
trevdev profile image
Trev

At what point do you decide that efficiency is more important, or even more achievable than readable, functional code?

I've got a situation where my data is structured a certain way and a nested for loop computes faster than the many, many iterations my attempt at the functional approach uses.

I've been told that functional programming scales well because of re-usable code. I really must be missing something.

Collapse
 
zulvkr profile image
zulvkr

In my understanding, for code with similar O(n) using map instead of for loop will incur small performance penalty.

How much is the O(n) in your case?

Collapse
 
richytong profile image
Richard Tong • Edited

For me at least, I have always believed there is an optimal (in every sense of the word) way to express everything in every context. Functional programming begins to solve that problem, and JavaScript solves that problem on another level.

The truth is there will be times when it's more optimal and readable to write imperative style code (for loops, let index ..., etc). I'm partial to while loops myself. That's the beauty of JavaScript - you can mix paradigms and apply them where it makes sense. I think it requires trying things out and evaluating everything as honestly as possible.

When you say that functional programming scales well, do you mean that it scales well in the same sense that TypeScript scales well (largerish shared codebases)? I suppose one of the premises to functional programming is that there is such a way such that code is reused as optimally as possible, and by virtue of simplicity it would indeed scale well.