1

While advocating pure functions I'd like to also mention performance as an advantage and while I know that traditional functional languages heavily optimize them (I assume everything in haskell is magically memoized) I don't know what the case is for JS.

I have written a small jsperf which does prove that a non-trivial pure function is way faster than a side-effect-y one but the benchmark may be completely broken. (http://jsperf.com/automatic-memoization)

adrianton3
  • 2,258
  • 3
  • 20
  • 33
  • Maybe better suited for http://programmers.stackexchange.com/, not sure. – Felix Kling Dec 11 '14 at 19:00
  • I suspect the performance difference here is due to the performance cost global-scope variable access. It's costly to look up the `window` object and get a property on that object and then save a new value in it. If you use `var g` and `g++` instead of `window.g` and `window.g++`, the performance between the two is much closer: http://jsperf.com/automatic-memoization/2 – apsillers Dec 11 '14 at 19:17
  • Things in Haskell are definitely not automaitcally memoized... quite te contrary, GHC mostly doesn't even perform simple forms of "memoization" such as subcommon expression optimization. Such a shame IMO. – MaiaVictor May 12 '15 at 22:03

0 Answers0