Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve perf #764

Merged
merged 2 commits into from Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lint-staged.config.js
@@ -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
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
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
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
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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know anything about the packae and these things, so please tell me if I'm off :)

I find it strange and not type-sae at all that the function we pass returns any. Also I doubt we can pass undefined as a function because it's called directly...
So shouldn't the type be more something like:

export function memoize<T>(fn: () => T): () => T;

Maybe you wanted undefined because of the line fn = undefined; in the function. It might be better (for types) to keep fn in a separate variable that can be undefined, so that the parameter itself can't be.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, yeah, if you want improve types here - feel free to send a PR

/**
* @template T
* @param fn {(function(): any) | undefined}
* @returns {function(): Promise<T>}
*/
export function asyncMemoize<T>(fn: (() => any) | undefined): () => Promise<T>;