Perf: reduce infra runtime costs

This commit is contained in:
SukkaW
2023-09-14 21:11:43 +08:00
parent 0a4c99ab0e
commit b42bd05ccf
23 changed files with 172 additions and 265 deletions

View File

@@ -13,11 +13,12 @@ module.exports.parse = (domain) => {
return cache.sync(domain, () => tldts.parse(domain, sharedConfig));
};
const gothillGetDomainCache = createCache('cached-gorhill-get-domain', true);
let gothillGetDomainCache = null;
/**
* @param {import('gorhill-publicsuffixlist').default | null} gorhill
*/
module.exports.createCachedGorhillGetDomain = (gorhill) => {
gothillGetDomainCache ||= createCache('cached-gorhill-get-domain', true);
/**
* @param {string} domain
*/

View File

@@ -2,7 +2,7 @@ const { toASCII } = require('punycode/');
const fs = require('fs');
const path = require('path');
const publicSuffixPath = path.resolve(__dirname, '../../node_modules/.cache/public_suffix-list_dat.txt');
const publicSuffixPath = path.resolve(__dirname, '../../node_modules/.cache/public_suffix_list_dat.txt');
const getPublicSuffixListDat = () => {
if (fs.existsSync(publicSuffixPath)) {
return fs.promises.readFile(publicSuffixPath, 'utf-8');

View File

@@ -1,3 +1,4 @@
// @ts-check
const path = require('path');
const { performance } = require('perf_hooks');
@@ -35,8 +36,21 @@ module.exports.traceAsync = traceAsync;
* @template T
* @param {string} __filename
* @param {() => Promise<T>} fn
* @returns {T}
* @returns {Promise<T>}
*/
module.exports.runner = async (__filename, fn) => {
return traceAsync(`⌛ [${path.basename(__filename, path.extname(__filename))}]`, fn);
};
/**
* @template T
* @param {string} __filename
* @param {() => Promise<T>} fn
*/
module.exports.task = (__filename, fn) => {
const taskName = path.basename(__filename, path.extname(__filename));
return () => {
console.log(`🏃 [${taskName}] Start executing`);
return traceAsync(`✅ [${taskName}] Executed successfully`, fn);
};
};