Function-level Caching in JavaScript using Memoization

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ม.ค. 2025

ความคิดเห็น • 19

  • @raysunqi
    @raysunqi 3 ปีที่แล้ว +1

    This is my FE engineer interview question, thanks for sharing!

    • @LiveYourCode
      @LiveYourCode  3 ปีที่แล้ว +1

      glad to be of help! seems like the people in that company got skills! take the job! :)

  • @missizzyc
    @missizzyc 7 ปีที่แล้ว +2

    Ah best explanation of this technique, the simple first example helped me finally understand what the computer is doing when it's making those recursive calls...

  • @curiousprogrammer90
    @curiousprogrammer90 6 ปีที่แล้ว +1

    Very good explanation, thanks!

  • @mc-qf4bp
    @mc-qf4bp 7 ปีที่แล้ว +1

    Great video.

  • @DanielRamBeats
    @DanielRamBeats 7 ปีที่แล้ว +3

    This makes sense, but for those just learning this technique. Rather than using an array, it would make sense to use an object literal right? As the function being memoized can only ever have one output for the same input, using an object literal would ensure no duplicate keys are every possible and makes it more descriptive to the person reading the code. Also, in cases where the input is not a number, it a key property associated to the calculation would also be more flexible.

    • @LiveYourCode
      @LiveYourCode  7 ปีที่แล้ว +2

      In cases where the input is not a number, the "array" solution is the object solution, since cache['abc'] is the same as cache.abc in javascript. Still, this is only a starting point and there's a lot you can do to further enhance the memoizer.
      Also you pointed out a very important thing: This only makes sense for functions that only return one possible (output) value when called with the same input value. If the return value depends on somthing "outside" the function (like a global variable, a user input, the current date/time, a random number, etc.) the memoization doesn't make any sense. It's worth knowing the term "pure function" ... this really opened my eyes when I first heard about it :) en.wikipedia.org/wiki/Pure_function

  • @javascriptduniya1201
    @javascriptduniya1201 2 ปีที่แล้ว

    var cache =[] is setting empty every time here when function memorizer is called?

  • @RogerKeulen
    @RogerKeulen 7 ปีที่แล้ว +1

    For fib(n) memorization makes scense. But code generation and pre-calculation could also be a sollution for this example.

  • @saikoushik3812
    @saikoushik3812 5 ปีที่แล้ว +1

    How to get execution time? I mean any in-built js function?

    • @owlcitizen314
      @owlcitizen314 5 ปีที่แล้ว

      Sai Koushik there’s the performance api

    • @LiveYourCode
      @LiveYourCode  3 ปีที่แล้ว

      one very simple way is to use console.time and console.timeEnd:
      console.time('abc');
      ... do something ...
      console.timeEnd('abc');

  • @kollerjon
    @kollerjon 8 ปีที่แล้ว +1

    Hey, why do you turn n into a string? Is it strictly necessary?

    • @LiveYourCode
      @LiveYourCode  8 ปีที่แล้ว +3

      Hey,
      I assumed that when working with strings, javascript does not allocate that much space. Like when you write cache['4'] = 'abc' the array's length would be just 1 instead of 5 (by allocating the slots 0,1,2,3,4). But when I try this out in chrome, it still seems to allocate 5 slots even when a string is given as index.
      So actually: No, it is not necessary! And as from what I see now it doesn't provide any benefit. (in contrast: it could even be problematic if n is an object for example). I'm gonna create an annotation in the video. Thanks for asking! :)

  • @dantewhite7659
    @dantewhite7659 4 ปีที่แล้ว

    here because i want a better understanding of how to use Apollo,

  • @xanaduzhang186
    @xanaduzhang186 2 ปีที่แล้ว

    ```
    const memorizer = f => { .. .. .. .. .. .. };
    const _slowFun = n => n+1;
    const slowFun = memorizer(_slowFun};
    export { slowFun };
    ```

  • @xanaduzhang186
    @xanaduzhang186 2 ปีที่แล้ว

    ```
    const memorizer = (f, exp) => { if(exp).. .. .. .. .. .. };
    const _slowFun = n => n+1;
    const slowFun = (n, exp=false) => memorizer(_slowFun, exp} ;
    export { slowFun };
    ```