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

Optimize findManifestDependencies #247

Merged
merged 2 commits into from
Sep 19, 2023

Conversation

Connormiha
Copy link
Contributor

Spread creates a new array in each iteration, resulting in a complexity of O(n*n). Using push has a complexity of O(n) and does not create a new array.

Comment on lines 19 to 24
const scripts = Object.entries(manifest.scripts ?? {}).reduce((scripts, [scriptName, script]) => {
if (script && (scriptFilter.length === 0 || scriptFilter.includes(scriptName))) {
return [...scripts, script];
scripts.push(script);
}
return scripts;
}, [] as string[]);
Copy link

Choose a reason for hiding this comment

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

FWIW, this is more or less what flatMap's made for

  const scripts = Object.entries(manifest.scripts ?? {}).flatMap(([scriptName, script]) => {
    if (script && (scriptFilter.length === 0 || scriptFilter.includes(scriptName))) {
      return [script];
    }

    return [];
  });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this code should be slower, because we still create new array.

@@ -18,7 +18,7 @@ const findManifestDependencies = async ({ manifest, isProduction, isStrict, dir,

const scripts = Object.entries(manifest.scripts ?? {}).reduce((scripts, [scriptName, script]) => {
if (script && (scriptFilter.length === 0 || scriptFilter.includes(scriptName))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you wanted to micro-optimise this for allocations a bit further, you could move scriptFilter out of the function and make it a set so that it has faster .has() checks, and also is only allocated once. Then rather than conditionally setting it to either ['start', 'posinstall'] or [], move the isProduction check down here. So scriptFilter becomes const scriptFilter = new Set(['start', 'postinstall']); outside of the function, and this check becomes if (script && (!isProduction || scriptFilter.has(scriptName))) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suggest making additions a bit later. I try not to make too many changes in the PR to reduce the likelihood of a bug. Moving the "isProduction" check is already a significant change, as it is already checked in another component. @webpro

Copy link
Collaborator

@webpro webpro Sep 19, 2023

Choose a reason for hiding this comment

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

That's fine, but don't worry too much! We have tests, e.g. the isProduction flag with the start script has coverage.

@webpro
Copy link
Collaborator

webpro commented Sep 19, 2023

Premature optimizations if you'd ask me (most time is spent during with disk IO and creating (and traversing) ASTs), it's still fun to implement and learn from each other here, so feel free to continue. I'm glad Knip receives also this kind of attention for detail! ❤️

I would prefer @me4502's suggestion, but tbh happy to merge any of the ideas here.

@webpro
Copy link
Collaborator

webpro commented Sep 19, 2023

Alright, merging this, thanks @Connormiha et al!

@webpro webpro merged commit 43b68a8 into webpro-nl:main Sep 19, 2023
9 checks passed
@webpro
Copy link
Collaborator

webpro commented Sep 24, 2023

🚀 This pull request is included in v2.26.0. See Release 2.26.0 for release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants