Skip to content

Commit

Permalink
implement memoization for domain normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Viet Huynh committed Nov 19, 2023
1 parent b364df7 commit 7189599
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cf_list_create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
extractDomain,
isComment,
isValidDomain,
memoize,
readFile,
} from "./lib/utils.js";

Expand All @@ -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}`);
Expand All @@ -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;

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};

0 comments on commit 7189599

Please sign in to comment.