Chapter 7. Functional optimizations

 

This chapter covers

  • Indicating where functional code is performant
  • Examining the internals of JavaScript function execution
  • Implications of nesting function contexts and recursion
  • Optimizing function evaluation with lazy evaluation
  • Speeding up program execution with memoization
  • Unwinding recursive calls with tail recursive functions

We should forget about small efficiencies, say about 97% of the time ... premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Donald Knuth, The Art of Computer Programming

Always optimize last, or so they say. In previous chapters, you learned how to write and test your functional code; and now, nearing the end of this wonderful journey, we look at ways to optimize it. No single programming paradigm is the Holy Grail, and each has its share of trade-offs: performance versus abstraction, for example. Functional programming provides layers of abstractions around your code to achieve its high level of fluency and declarativeness. With all of this internal currying, recursion, and monadic wrapping composed together to solve even the simplest types of problems, you may wonder, “Is functional code as performant as imperative code?”

7.1. Under the hood of function execution

7.2. Deferring execution using lazy evaluation

7.3. Implementing a call-when-needed strategy

7.4. Recursion and tail-call optimization (TCO)

7.5. Summary

sitemap