implement memoization for domain normalization

This commit is contained in:
Viet Huynh
2023-11-19 18:20:47 +07:00
parent b364df7864
commit 7189599019
2 changed files with 25 additions and 2 deletions
+21
View File
@@ -90,3 +90,24 @@ export const readFile = async (filePath, onLine) => {
throw err;
}
};
/**
* Memoizes a function
* @template T The argument type of the function.
* @template R The return type of the function.
* @param {(...fnArgs: T[]) => R} fn The function to be memoized.
*/
export const memoize = (fn) => {
const cache = new Map();
return (...args) => {
const key = args.join("-");
if (cache.has(key)) return cache.get(key);
const result = fn(...args);
cache.set(key, result);
return result;
};
};