A simple introduction to Memoization in JavaScript, including its concept, workflow, and a practical example.
This repository contains a PDF that explains how Memoization can improve the performance of functions by storing previously calculated results in a cache.
The PDF covers:
- What is Memoization?
- How Memoization works
- The role of caching
- A practical JavaScript implementation
- Executing a memoized function
- Comparing the first and subsequent function calls
Memoization is an optimization technique commonly used in functional programming.
It stores the results of previous function calls in a cache. When the function is called again with the same input, the cached result can be returned instead of performing the calculation again.
Function Call
β
Check Cache
β β
Found Not Found
β β
Return Calculate
Result β
Store Result
β
Return Result
The PDF includes a simple memoization example:
const memoizeAddition = () => {
let cache = {};
return (value) => {
if (value in cache) {
console.log("Fetching from cache");
return cache[value];
} else {
console.log("Calculating result");
let result = value + 20;
cache[value] = result;
return result;
}
};
};const addition = memoizeAddition();
console.log(addition(20)); // 40 β Calculating result
console.log(addition(20)); // 40 β Fetching from cacheOn the first execution, the result is calculated and stored in the cache.
On the second execution with the same input, the stored result is returned directly.
You can find the complete explanation in the PDF included in this repository:
π Download and Read Memoization in JavaScript
Memoization can reduce unnecessary repeated calculations by storing previously computed results and reusing them when the same input occurs again.
Calculate once, reuse when possible.
Made for learning and understanding Memoization in JavaScript.