Memoization caches a function’s result under a key made from its arguments. When the function receives the same inputs again, it returns the stored result instead of recomputing it. This is most useful for pure, computationally expensive functions whose outputs stay consistent for the same inputs. section 7.3 section 7.5
A typical memoization flow is:
- Create a cache key from the function’s arguments. section 7.3
Memoizing ROT13 and factorial
A JavaScript function can be wrapped with memoize so repeated calls reuse prior work: var rot13 = (s => s.replace(/[a-zA-Z]/g, c => String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26))).memoize(); rot13('functional_js_50_off'); rot13('functional_js_50_off'); The first call computes and stores the result; the second returns it from the cache. The same idea helps recursive factorial: after factorial(100) is cached, factorial(101) can reuse that intermediate result instead of rebuilding the entire computation.







