← Back to list

Memoization

Memoization is a functional programming technique which attempts to increase a function’s performance by caching its previously computed…

Ayush · 2026-05-13 14:11 · 0 claps · 0.6 min read
#javascript #memoization #interview-questions #learning #data-structures
Open on Medium ↗
Wiki topics: EDU · Education & Learning 💻 · Programming 🌐 · Web Development

Memoization

Memoization is a functional programming technique which attempts to increase a function’s performance by caching its previously computed results. Each time a memoized function is called, its parameters are used to index the cache. If the data is present, then it can be returned, without executing the entire function. Otherwise the function is executed and then the result is added to the cache.

Let’s take an example of adding function with memoization,

const memoizeAddition = () => {
  let cache = {};
  return (value) => {
    if (value in cache) {
      console.log("Fetching from cache");
      return cache[value]; // Here, cache.value cannot be used as property name starts with the number which is not a valid JavaScript  identifier. Hence, can only be accessed using the square bracket notation.
    } else {
      console.log("Calculating result");
      let result = value + 20;
      cache[value] = result;
      return result;
    }
  };
};
// returned function from memoizeAddition
const addition = memoizeAddition();
console.log(addition(20)); //output: 40 calculated
console.log(addition(20)); //output: 40 cached

메타데이터
post_id
77a407a9f8ea
slug
memoization-77a407a9f8ea
url
https://medium.com/@cwayush/memoization-77a407a9f8ea
canonical_url
https://medium.com/@cwayush/memoization-77a407a9f8ea
author_url
https://medium.com/@cwayush
status
ok
fetched_at
2026-08-19 11:02:14