Ad
Functions
Control Flow
Basic Language Features
Fundamentals

This solution uses a simple hash for holding stringified args to quickly lookup the cache.

The assumption is that only one type of function signature is ever used.

Functions
Control Flow
Basic Language Features
Fundamentals

This solution uses a simple hash for holding stringified args to quickly lookup the cache.

The assumption is that only one type of function signature is ever used.

Code
Diff
  • var lookup = {};
    
    function memoize(func) {
      return (...args) => {
        // check cache hit
        if (!lookup[args.toString()]) {
          lookup[args.toString()] = func.apply(this, args);
        }
    
        return lookup[args.toString()];
      };
    }
    • var lookup = {};
    • function memoize(func) {
    • return (...args) => {
    • // your function
    • // check cache hit
    • if (!lookup[args.toString()]) {
    • lookup[args.toString()] = func.apply(this, args);
    • }
    • return lookup[args.toString()];
    • };
    • }