MCPcopy
hub / github.com/final-form/final-form / memoize

Function memoize

src/memoize.ts:3–17  ·  view source on GitHub ↗
(fn: T)

Source from the content-addressed store, hash-verified

1import shallowEqual from "./shallowEqual";
2
3const memoize = <T extends (...args: any[]) => any>(fn: T): T => {
4 let lastArgs: any[] | undefined;
5 let lastResult: any;
6 return ((...args: any[]) => {
7 if (
8 !lastArgs ||
9 args.length !== lastArgs.length ||
10 args.some((arg, index) => !shallowEqual(lastArgs![index], arg))
11 ) {
12 lastArgs = args;
13 lastResult = fn(...args);
14 }
15 return lastResult;
16 }) as T;
17};
18
19export default memoize;

Callers 2

memoize.test.tsFile · 0.85
createFormFunction · 0.85

Calls 1

shallowEqualFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…