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
+4 -2
View File
@@ -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
+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;
};
};