Memoizes the function result so it is not computed for the same parameters. Uses the given equality function.

Type signature

<T>(equals: (x: T[], y: T[]) => boolean) => <TResult>(f: (...xs: T[]) => TResult) => (...args: T[]) => TResult

Examples

const f = ({ x }) => {
  console.log(x);
  return x + 1;
};

const memoized = memoizeWith(
  (a, b) => a.x === b.x,
)(f);

memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 3 });
Try in REPL

Questions

  • How to memoize a function with a custom equality function?

TypeScript sourceJavaScript source