Skip to Content
Mastering JavaScript Functional Programming - Second Edition
book

Mastering JavaScript Functional Programming - Second Edition

by Federico Kereki
January 2020
Intermediate to advanced
470 pages
11h 13m
English
Packt Publishing
Content preview from Mastering JavaScript Functional Programming - Second Edition

Memoization

Since the output of a pure function for a given input is always the same, you can cache the function results and avoid a possibly costly recalculation. This process, which implies evaluating an expression only the first time and caching the result for later calls, is called memoization

We will come back to this idea in Chapter 6, Producing Functions – Higher-Order Functions, but let's look at an example done by hand. The Fibonacci sequence is always used for this example because of its simplicity and its hidden calculation costs. This sequence is defined as follows:

  • For n=0, fib(n)=0
  • For n=1, fib(n)=1
  • For n>1, fib(n)=fib(n-2)+fib(n-1)
Fibonacci's name actually comes from filius Bonacci, or son of Bonacci. He is best known ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering JavaScript Functional Programming

Mastering JavaScript Functional Programming

Federico Kereki

Publisher Resources

ISBN: 9781839213069Supplemental Content