🔥 Learn Memosation in JavaScript 🔥 | Simplest explanation Ever | Most important frontEnd Int topic
HTML-код
- Опубликовано: 27 янв 2025
- Join Uncommon Geeks community to discuss with other developers: t.me/uncommongeek. Definition: It is an optimisation technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again
Interview Preparation series : • Watch this series and ...
My medium blog on memoisation: / memoization-in-javascr...
Medium Blog / mevasanth
Memoisation Github repository: github.com/coo...
Follow me on LinkedIn - / vasanth-bhat-4180909b
Github Repository that contains examples: github.com/coo...
JavaScript Custom implementation introduction: • Learn Custom implement...
MAANG series for frontEnd Developer: • First ever MAANG serie... - Наука
Hi Vasanth,
I am a subscriber of your channel, watching all your series. It has very good content and is really helpful. One thing i need to say her in this Fibonacci with memosation example the final code is
function fibonacci(num, cache = {}) {
if (cache[num]) return cache[num];
if (num === 0) return 0;
if (num === 1 || num === 2) return 1;
return cache[num] = fibonacci(num - 1) + fibonacci(num - 2);
}
fibonacci(10);
In above code you have missed the cache to pass into fibonacci recursive function. I realized when I run this code with fibonacci(50) it hangs-up 😃 the app.
return cache[num] = fibonacci(num - 1) + fibonacci(num - 2);
change to
return cache[num] = fibonacci(num - 1, cache) + fibonacci(num - 2, cache);
you have added correct code into your medium blog properly.
Correct code:
function fibonacci(num, cache = {}) {
if (cache[num]) return cache[num];
if (num === 0) return 0;
if (num === 1 || num === 2) return 1;
return cache[num] = fibonacci(num - 1, cache) + fibonacci(num - 2, cache);
}
fibonacci(10);
I think we need to pass cache as an argument to fibonacci function in return statement
Thanks for commenting. In case if you not subscribed to my channel please subscribe and watch all my videos. Coming to your question, can you say why we need to do so ?
@@careerwithvasanth because it will take a new empty object (default) everytime?
@@csedata3986 the empty object is required for internal processing and not necessarily need to passed from the caller. It will not become empty every time, only if you don't pass any value it is empty. Second iteration onwards the recursive function will pass values, so cache will not be empty. Hope that answers your question.
@@careerwithvasanth sorry, but CSE Data is right. Please add 'console.log(cache)' at first line of your 'fibonacci' function and run it again. You will see, that cache is empty in every iteration of function.
You will have to change last return line to 'return cache[num] = fibonacci(num-1, cache) + fibonacci(num-2, cache);' for cache to work.
Nice video on Memoisation
Thank you
Wow that was nice and clear explanation, thanks vasant 😊
My pleasure 😊 share channel details with you're friends and colleagues
Well explained , underrated channel for frontend dev
Thanks for commenting. In case if you not subscribed to my channel please subscribe and watch all my videos. Also, please share channel details with your friends.
BigInt can be used to store integer value which exceeds 53 bit, So factorial of 20 value can be stored in BigInt
Correct !! right answer !! have you ever used it in your project ?
Yes Vasanth, I have used BigInt often, Since we primarily work with binaries where the use cases are more
really deep explaiantion sir, thank u
hai sir..u r doing good job.
your videos are really helpful.plz cover more questions on the frontend development part
Sure I will
your videos are amazing bro
When I console the cache for fibonacci example inside the function it is always empty Can anyone explain why
It will be difficult to explain the solution here. Join our 3000+ member uncommon geeks telegram group here, you can discuss such queries there
t.me/uncommongeek install telegram app on your mobile and click this link. this would be a great place to discuss questions link this. Also, you will get a good community, monitored by me.
Helpful video. Thanks bro
Always welcome !! please share the channel details with your friends.
good explaination.
Thank you
Bro.. keeping the "cache" and setting the default parameter in the Fibonacci function declaration itself will not work properly right? because when the function execution ends all those local variables will be removed from the call stack hence in the next function call new local variables are created. Therefore we have to keep the cache = {} outside of the Fibonacci function.
const fact_cache = {0:1,1:1}
const factorial = (num) =>{
console.log(fact_cache);
if (fact_cache[num]){
console.log("already computed "+ num + "in cache")
return fact_cache[num];
}
else{
console.log("calulating the value");
fact_cache[num] = num * factorial(num-1);
return fact_cache[num];
}
}
well explained.
let cache = { 0:1, 1:1, 2:2 }
function factorial(n){
if(cache[n]){
return cache[n];
} else {
return cache[n] = n * factorial(n-1);
}
}
thank you sir
Most welcome
let factorials = {};
function factorial (num){
console.log(factorials);
if(num==1){
factorials[num]=1;
return 1;
}
else if(factorials[num]){
console.log('called');
return factorials[num];
}
else{
factorials[num] = num*factorial(num-1);
return num*factorial(num-1);
}
}
console.log(factorial(3));
appreciate the comments
let cache = [1]
function factorialMemo(x) {
if(!cache[x]) {
cache[x] = x * factorialMemo(x-1)
} else {
//Cache hit
console.log('Cache Hit')
}
return cache[x];
}
Thanks for commenting. In case if you not subscribed to my channel please subscribe and press the bell icon and watch all my videos and share the channel with your friends.
Using Memoisation approach :
function fact(num,cache={}) {
if(cache[num]) return cache[num];
if(num===0 || num===1) return 1;
return cache[num]= num*fact(num-1)
}
console.log(fact(5));
output: 120
let cache={}
const factorial = (num) => {
if (cache[num] !== undefined) {
console.log(`Using cache for ${num}`);
return cache[num];
}
if (num === 0 || num === 1) return 1;
let res = num * factorial(num - 1);
cache[num] = res;
return res;
}
console.log(factorial(5))
console.log(factorial(6))
console.log(factorial(8))
bigInt