Skip to content

Latest commit

Β 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 

Repository files navigation

Memoization in JavaScript

A simple introduction to Memoization in JavaScript, including its concept, workflow, and a practical example.

πŸ“„ About

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

πŸ’‘ What is Memoization?

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.

Basic Flow

Function Call
     ↓
Check Cache
   ↙     β†˜
Found    Not Found
  ↓         ↓
Return    Calculate
Result       ↓
          Store Result
              ↓
          Return Result

πŸ§‘β€πŸ’» JavaScript Example

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;
    }
  };
};

Usage

const addition = memoizeAddition();

console.log(addition(20)); // 40 β†’ Calculating result
console.log(addition(20)); // 40 β†’ Fetching from cache

On 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.

πŸ“š PDF

You can find the complete explanation in the PDF included in this repository:

πŸ“„ Download and Read Memoization in JavaScript

🎯 Key Takeaway

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors