@blinkk/degu
    Preparing search index...

    Function memoize

    • Memoizes a function. If the params passed to the function are the same as a previous call, then the cached results are returned, saving an unncessary excecution.

      Consider the follwing example

      let expensiveCalculation = func.memoize((a,b,c)=> {
      return a + b + c;
      })

      expensiveCalculation(1, 1, 1); // 3, callback executed and results cached.
      expensiveCalculation(1, 1, 1); // 3 from cache
      expensiveCalculation(1, 1, 1); // 3 from cache

      expensiveCalculation(2, 2, 2); // 6, callback executed and results cached.
      expensiveCalculation(2, 2, 2); // 6 from cache

      expensiveCalculation(1, 1, 1); // 3 from cache
      expensiveCalculation(2, 2, 2); // 6 from cache

      A common case for memoize might be to use it in a class method. You can implement that with this pattern.

      class MyClass {

      constructor() {
      this.calculate = func.memoize(this.calculate.bind(this)) as any;
      }

      calculate(a, b) {
      return a + b;
      }
      }

      Parameters

      • callback: Function

        The function to wrap the memoize mechanism. It's expect that this function returns something. Can't be void.

      Returns Function