MCPcopy Create free account
hub / github.com/Snapchat/Valdi / memoize

Function memoize

valdi/vscode_debugger/src/common/objUtils.ts:215–231  ·  view source on GitHub ↗
(fn: (arg: T) => R)

Source from the content-addressed store, hash-verified

213 * Memoizes the single-parameter function.
214 */
215export function memoize<T, R>(fn: (arg: T) => R): ((arg: T) => R) & { clear(): void } {
216 const cached = new Map<T, R>();
217 const wrapper = (arg: T): R => {
218 if (cached.has(arg)) {
219 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
220 return cached.get(arg)!;
221 }
222
223 const value = fn(arg);
224 cached.set(arg, value);
225 return value;
226 };
227
228 wrapper.clear = () => cached.clear();
229
230 return wrapper;
231}
232
233/**
234 * Debounces the function call for an interval.

Callers 2

constructInspectorWSUriFunction · 0.90
urlUtils.tsFile · 0.90

Calls 1

clearMethod · 0.65

Tested by

no test coverage detected