Memoizee instance method in node Class

3.9k views Asked by At

I'm seeking an elegant way to memoize a class function using Memoizee package.

Outside a class, you can go about this trivially:

const memoize = require('memoizee')

const myFunc = memoize(function myfunc(){ ... })

but inside a class block, this won't work:

class foo {
    constructor(){ ... }

    // Without memoization you would do:
    myFunc(){ ... }

    // Can't do this here:
    myFunc = memoize(function myfunc(){ ... })
}

I can think of creating it in the constructor using this. syntax, but this will result in a less uniform class definition, as non-memoized methods will be declared outside the constructor:

class foo {
    constructor(){
        // Inside for memoized:
        this.myFunc = memoize(function myfunc(){ ... }) 
    }

    // Outside for non-memoized:
    otherFunc(){ ... }
}

How would you wrap an instance method?

3

There are 3 answers

1
Ilya Kozhevnikov On BEST ANSWER

It's possible to overwrite own method definition in the constructor

class Foo {
  constructor() {
    this.bar = _.memoize(this.bar);
  }

  bar(key) {
    return `${key} = ${Math.random()}`;
  }
}

const foo = new Foo();
console.log(foo.bar(1));
console.log(foo.bar(1));
console.log(foo.bar(2));
console.log(foo.bar(2));


// Output: 
1 = 0.6701435727286942
1 = 0.6701435727286942
2 = 0.38438568145894747
2 = 0.38438568145894747
1
rsp On

Depending on the way you run your code and whether or not you're using transpilation steps, maybe you can use the memoized-class-decorator with:

class foo {
    constructor () { ... }

    // Without memoization:
    myFunc () { ... }

    // With memoization:
    @memoize
    myFunc () { ... }
}
0
Mariusz Nowak On

There is a dedicated handling for methods in memoizee. See: https://github.com/medikoo/memoizee#memoizing-methods

Still it won't work with native class syntax, best you can do at this point is something as:

const memoizeMethods = require('memoizee/methods');

class Foo {
  // .. non memoized definitions
}
Object.defineProperties(Foo.prototype, memoizeMethods({
  // Memoized definitions, need to be provided via descriptors.
  // To make it less verbose you can use packages as 'd':
  // https://www.npmjs.com/package/d
  myFunc: {
    configurable: true,
    writable: true,
    enumerable: false,
    value: function () { ... }
 }
});