From 7189599019d26c220d2fa2d39be5c6daa60e8a19 Mon Sep 17 00:00:00 2001 From: Viet Huynh Date: Sun, 19 Nov 2023 18:20:47 +0700 Subject: [PATCH] implement memoization for domain normalization --- cf_list_create.js | 6 ++++-- lib/utils.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/cf_list_create.js b/cf_list_create.js index cfecb22..bce748b 100644 --- a/cf_list_create.js +++ b/cf_list_create.js @@ -17,6 +17,7 @@ import { extractDomain, isComment, isValidDomain, + memoize, readFile, } from "./lib/utils.js"; @@ -33,6 +34,7 @@ let processedDomainCount = 0; let unnecessaryDomainCount = 0; let duplicateDomainCount = 0; let allowedDomainCount = 0; +const memoizedNormalizeDomain = memoize(normalizeDomain); // Read allowlist console.log(`Processing ${allowlistFilename}`); @@ -43,7 +45,7 @@ await readFile(resolve(`./${allowlistFilename}`), (line) => { if (isComment(_line)) return; - const domain = normalizeDomain(_line, true); + const domain = memoizedNormalizeDomain(_line, true); if (!isValidDomain(domain)) return; @@ -65,7 +67,7 @@ await readFile(resolve(`./${blocklistFilename}`), (line, rl) => { if (isComment(_line)) return; // Remove prefixes and suffixes in hosts, wildcard or adblock format - const domain = normalizeDomain(_line); + const domain = memoizedNormalizeDomain(_line); // Check if it is a valid domain which is not a URL or does not contain // characters like * in the middle of the domain diff --git a/lib/utils.js b/lib/utils.js index 48cee7c..dd91e21 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; + }; +};