DEV Community

Nikunj R. Prajapati
Nikunj R. Prajapati

Posted on

3 2

Day 4 : What is memoization in javascript ?

  • Memoization is a cache technique that makes application performance faster by storing the results of expensive function calls.

  • In simple terms it's storing in memory, let's understand it this way,

Example

function someIntensiveDBTask(dependsOnThis){

// Lot's of calculations here, will return same output for same input

}
Enter fullscreen mode Exit fullscreen mode

So in that kind of scenario we can implement Memoization, Let's understand it.

const memo = []
function someIntensiveDBTask(dependsOnThis){

if(memo[dependsOnThis]) return memo[dependsOnThis];

// else part
// Do some calculations...
// ..
// ..
// ..

// we are caching result before returning here and storing it in memo so next time we can use it directly.

memo[dependsOnThis] = result; 

return result

}
Enter fullscreen mode Exit fullscreen mode

Memoization is a way to lower a function’s time cost in exchange for space cost.

Resources :
Res 1
Res 2

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay