fix(cache): use cacheNoArgs for better performance

This commit is contained in:
JellyBrick
2024-08-04 16:31:07 +09:00
parent ac51f798c3
commit 59a5679cbb
7 changed files with 36 additions and 27 deletions

View File

@ -40,6 +40,16 @@ export function cache<T extends (...params: P) => R, P extends never[], R>(
}) as T;
}
export function cacheNoArgs<R>(fn: () => R): () => R {
let cached: R;
return () => {
if (cached === undefined) {
cached = fn();
}
return cached;
};
}
/*
The following are currently unused, but potentially useful in the future
*/