🔥 Learn Memosation in JavaScript 🔥 | Simplest explanation Ever | Most important frontEnd Int topic

  Рет қаралды 4,657

Vasanth Bhat

Vasanth Bhat

Күн бұрын

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

Пікірлер: 40
@vishalkhoje
@vishalkhoje 2 жыл бұрын
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);
@life.karma.growth
@life.karma.growth 2 жыл бұрын
Well explained , underrated channel for frontend dev
@careerwithvasanth
@careerwithvasanth 2 жыл бұрын
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.
@vikasni95
@vikasni95 Жыл бұрын
Wow that was nice and clear explanation, thanks vasant 😊
@careerwithvasanth
@careerwithvasanth Жыл бұрын
My pleasure 😊 share channel details with you're friends and colleagues
@jagannatarajan4972
@jagannatarajan4972 9 ай бұрын
your videos are amazing bro
@rashidsiddiqui4502
@rashidsiddiqui4502 Жыл бұрын
really deep explaiantion sir, thank u
@ajaykathait7633
@ajaykathait7633 4 ай бұрын
Nice video on Memoisation
@careerwithvasanth
@careerwithvasanth 4 ай бұрын
Thank you
@frontend-cafe7166
@frontend-cafe7166 2 жыл бұрын
hai sir..u r doing good job. your videos are really helpful.plz cover more questions on the frontend development part
@careerwithvasanth
@careerwithvasanth 2 жыл бұрын
Sure I will
@DilipKumar-hn4oy
@DilipKumar-hn4oy Жыл бұрын
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]; } }
@madhanrock5390
@madhanrock5390 Жыл бұрын
BigInt can be used to store integer value which exceeds 53 bit, So factorial of 20 value can be stored in BigInt
@careerwithvasanth
@careerwithvasanth Жыл бұрын
Correct !! right answer !! have you ever used it in your project ?
@madhanrock5390
@madhanrock5390 Жыл бұрын
Yes Vasanth, I have used BigInt often, Since we primarily work with binaries where the use cases are more
@csedata3986
@csedata3986 2 жыл бұрын
I think we need to pass cache as an argument to fibonacci function in return statement
@careerwithvasanth
@careerwithvasanth 2 жыл бұрын
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 ?
@csedata3986
@csedata3986 2 жыл бұрын
@@careerwithvasanth because it will take a new empty object (default) everytime?
@careerwithvasanth
@careerwithvasanth 2 жыл бұрын
@@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.
2 жыл бұрын
@@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.
@ArunDevadiga-mp5ni
@ArunDevadiga-mp5ni 5 ай бұрын
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.
@life_of_sangeetha_saradaram
@life_of_sangeetha_saradaram 2 жыл бұрын
Helpful video. Thanks bro
@careerwithvasanth
@careerwithvasanth 2 жыл бұрын
Always welcome !! please share the channel details with your friends.
@aniketpandey62
@aniketpandey62 Жыл бұрын
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); } }
@ishu4696
@ishu4696 11 ай бұрын
good explaination.
@careerwithvasanth
@careerwithvasanth 11 ай бұрын
Thank you
@prachijain6602
@prachijain6602 10 ай бұрын
well explained.
@KUMAILinUAE
@KUMAILinUAE 2 жыл бұрын
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
@aravindguna352
@aravindguna352 Жыл бұрын
When I console the cache for fibonacci example inside the function it is always empty Can anyone explain why
@careerwithvasanth
@careerwithvasanth Жыл бұрын
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.
@yogesh1893
@yogesh1893 Жыл бұрын
thank you sir
@careerwithvasanth
@careerwithvasanth Жыл бұрын
Most welcome
@tarunbhadauria9075
@tarunbhadauria9075 Жыл бұрын
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));
@careerwithvasanth
@careerwithvasanth Жыл бұрын
appreciate the comments
@subodhasahu2958
@subodhasahu2958 2 жыл бұрын
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]; }
@careerwithvasanth
@careerwithvasanth 2 жыл бұрын
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.
@SujitSinha-q8r
@SujitSinha-q8r 5 ай бұрын
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))
@rajeshkanna1829
@rajeshkanna1829 5 ай бұрын
bigInt
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
All useEffect Mistakes Every Junior React Developer Makes
22:23
Learn JavaScript Event Listeners In 18 Minutes
18:03
Web Dev Simplified
Рет қаралды 611 М.
The Science of Illusions with Teller
50:10
StarTalk
Рет қаралды 14 М.
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН