Lesson 23 — Memoize (expert)
Never compute the same thing twice
Memoization wraps a function with a cache: the first call with an input computes and stores the result; later calls with the same input return the stored result instantly. A Map is a good cache — has, get and set do exactly what they say. It only works for pure functions (same input → always the same output, no side effects).
Why it matters: expensive calculations (prices, layouts, recursive math) become instant on repeat.
Finish memoize: if the cache has x return it; otherwise compute fn(x), store it, and return it.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based JavaScript Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for JavaScript Foundations books — check the publication year so the examples match current versions.
Search Amazon →