🔥 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...
  • НаукаНаука

Комментарии • 40

  • @vishalkhoje
    @vishalkhoje 2 года назад +3

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

  • @csedata3986
    @csedata3986 2 года назад +4

    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 года назад +1

      @@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 года назад +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.

  • @ajaykathait7633
    @ajaykathait7633 4 месяца назад +1

    Nice video on Memoisation

  • @vikasni95
    @vikasni95 Год назад

    Wow that was nice and clear explanation, thanks vasant 😊

    • @careerwithvasanth
      @careerwithvasanth  Год назад

      My pleasure 😊 share channel details with you're friends and colleagues

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

  • @madhanrock5390
    @madhanrock5390 Год назад +1

    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

  • @rashidsiddiqui4502
    @rashidsiddiqui4502 Год назад

    really deep explaiantion sir, thank u

  • @frontend-cafe7166
    @frontend-cafe7166 2 года назад +1

    hai sir..u r doing good job.
    your videos are really helpful.plz cover more questions on the frontend development part

  • @jagannatarajan4972
    @jagannatarajan4972 8 месяцев назад

    your videos are amazing bro

  • @aravindguna352
    @aravindguna352 Год назад +1

    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.

  • @life_of_sangeetha_saradaram
    @life_of_sangeetha_saradaram 2 года назад +1

    Helpful video. Thanks bro

    • @careerwithvasanth
      @careerwithvasanth  2 года назад

      Always welcome !! please share the channel details with your friends.

  • @ishu4696
    @ishu4696 11 месяцев назад +1

    good explaination.

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

  • @DilipKumar-hn4oy
    @DilipKumar-hn4oy Год назад +2

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

  • @prachijain6602
    @prachijain6602 10 месяцев назад

    well explained.

  • @aniketpandey62
    @aniketpandey62 Год назад +1

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

  • @yogesh1893
    @yogesh1893 Год назад

    thank you sir

  • @tarunbhadauria9075
    @tarunbhadauria9075 Год назад +1

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

  • @subodhasahu2958
    @subodhasahu2958 2 года назад +1

    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.

  • @KUMAILinUAE
    @KUMAILinUAE 2 года назад +1

    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

  • @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 4 месяца назад

    bigInt