cached-expression
Expressions that are calculated once for each object
Usage
APIs
declare
Example
Unique Object Reference
const calculate = x Math const a0 = const a1 = const a2 = const b0 = // Same input produces same reference // Different inputs produces different reference
Application: Factorial
Caching improve performance when calculating factorial of multiple numbers.
// Definition:// fac(0) = 1// fac(n) = n * fac(n - 1)const fac = x < 2 ? 1 : x * calculate const result = 0 1 2 3 4
Application: Fibonacci
Caching turn recursive fibonacci algorithm from a wasteful binary tree model to a efficient pyramid model.
// Definition:// fib(0) = fib(1) = 1// fib(n) = fib(n - 2) + fib(n - 1)const fib = x < 2 ? 1 : + calculate const result =