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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return [...scripts, script];
scripts.push(script);
}
return scripts;
}, [] as string[]);
Comment on lines 19 to 24
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.

Expand Down