Skip to content

Commit

Permalink
fix: improve perf (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jan 17, 2024
1 parent 7ee03c9 commit a7379a9
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 45 deletions.
5 changes: 4 additions & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
"*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"],
"*": [
"prettier --cache --write --ignore-unknown",
"cspell --cache --no-must-find-files",
],
"*.js": ["eslint --cache --fix"],
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"build": "npm-run-all -p \"build:**\"",
"commitlint": "commitlint --from=master",
"security": "npm audit --production",
"lint:prettier": "prettier --list-different .",
"lint:prettier": "prettier --cache --list-different .",
"lint:js": "eslint --cache .",
"lint:spelling": "cspell --cache --quiet \"**/*.*\"",
"lint:spelling": "cspell --cache --no-must-find-files --quiet \"**/*.*\"",
"lint:types": "tsc --pretty --noEmit",
"lint": "npm-run-all -l -p \"lint:**\"",
"fix:js": "npm run lint:js -- --fix",
Expand Down
10 changes: 2 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ const { validate } = require("schema-utils");
const { version } = require("../package.json");

const schema = require("./options.json");
const {
readFile,
stat,
throttleAll,
memoize,
asyncMemoize,
} = require("./utils");
const { readFile, stat, throttleAll, memoize } = require("./utils");

const template = /\[\\*([\w:]+)\\*\]/i;

Expand All @@ -36,7 +30,7 @@ const getFastGlob = memoize(() =>
require("fast-glob"),
);

const getGlobby = asyncMemoize(async () => {
const getGlobby = memoize(async () => {
// @ts-ignore
const { globby } = await import("globby");

Expand Down
29 changes: 1 addition & 28 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,4 @@ function memoize(fn) {
};
}

/**
* @template T
* @param fn {(function(): any) | undefined}
* @returns {function(): Promise<T>}
*/
function asyncMemoize(fn) {
let cache = false;
/** @type {T} */
let result;

return async () => {
if (cache) {
return result;
}

result = await /** @type {function(): any} */ (fn)();
cache = true;

// Allow to clean up memory for fn
// and all dependent resources
// eslint-disable-next-line no-undefined, no-param-reassign
fn = undefined;

return result;
};
}

module.exports = { stat, readFile, throttleAll, memoize, asyncMemoize };
module.exports = { stat, readFile, throttleAll, memoize };
6 changes: 0 additions & 6 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,3 @@ export function throttleAll<T>(limit: number, tasks: Task<T>[]): Promise<T[]>;
* @returns {function(): T}
*/
export function memoize<T>(fn: (() => any) | undefined): () => T;
/**
* @template T
* @param fn {(function(): any) | undefined}
* @returns {function(): Promise<T>}
*/
export function asyncMemoize<T>(fn: (() => any) | undefined): () => Promise<T>;

0 comments on commit a7379a9

Please sign in to comment.