3 ms·
The problem with treating JS like a good functional language when it's absolutely a terrible one (not to mention a terrible language in general) is that you get
by kevingadd 13y ago
The problem with treating JS like a good functional language when it's absolutely a terrible one (not to mention a terrible language in general) is that you get badly-performing code riddled with subtle problems.
You build algorithms around assumed immutability, and then it's trivial to accidentally introduce mutation into them - sometimes entirely by accident, since third party JS libraries can introduce mutation into non-mutating operations. For example, I once pulled in a third-party JS 'printStackTrace' library for debugging purposes, then weeks later had to spend half a day troubleshooting to figure out that printStackTrace was mutating arbitrary arrays from the stack, thus permanently corrupting random globals.
If you're maintaining a large codebase with multiple people, the risk of accidentally introducing mutability is very, very real.
Faking tail calls in JS requires using constructs that introduce unnecessary GC pressure and prevent most JIT optimizations, compared to tail calls in a real functional environment where they deliver great performance without GC pressure.
Partial application and composition in JS performs like garbage, as mentioned above with faking tail calls. 'True' FP runtimes can do better here, as do some static runtimes (.NET, for example, has the ability to do a subset of composition and partial application without any GC or call overhead whatsoever.)
Your composeObj example is another good one. You've created this special variant of JS callables that isn't actually a function (because JS functions are rigid and hard to build composable primitives out of), and it contains patterns that subdue JIT optimization (Object.create, for one). Again, attempts to pretend that JS is a good functional language just produce ugly, underperforming, hard-to-maintain code.