Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

fisker
Copy link
Contributor

@fisker fisker commented May 10, 2025

When running test/options.js, which contains 3 commands: ava, npm, and git.

Before: fs.access called 428 times.
After: fs.access called 210 times.

@fisker fisker changed the title Reduce fs.access call Reduce fs.access() call May 10, 2025
Comment on lines +49 to +50
[cwd, ...parts].flatMap(part => exeExtensions
.map(extension => access(`${path.resolve(part, file)}${extension}`)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Swap the order, spread once instead of twice.

// eslint-disable-next-line no-return-assign
const memoize = function_ => (...arguments_) =>
// Use returned assignment to keep code small
EXE_MEMO[arguments_.join('\0')] ??= function_(...arguments_);
Copy link
Collaborator

@ehmicky ehmicky May 10, 2025

Choose a reason for hiding this comment

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

We probably should use a different cache object for each memoized function.
The cache key cannot collide here with those two memoized functions since they have a different arity, but just in case we were to re-use that memoize() helper in the future.

// Memoize the `mIsExe` and `fs.access`, for performance
const memoize = function_ => {
  const EXE_MEMO = {}
  // eslint-disable-next-line no-return-assign
  return (...arguments_) =>
	  // Use returned assignment to keep code small
	  EXE_MEMO[arguments_.join('\0')] ??= function_(...arguments_);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The cache key cannot be collide here with those two memoized functions since they have a different arity

That's exactly why I choose to use one object, I think it's fine to keep this way for now, it we want resue, we probably need update the logic here , since it only takes strings and use \0 as separator, other cases might not suit anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since it's "nano" 😄

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, but let's keep this in mind.

@ehmicky
Copy link
Collaborator

ehmicky commented May 10, 2025

Looks good @sindresorhus?

@sindresorhus sindresorhus merged commit 4d7a93f into sindresorhus:main May 10, 2025
6 checks passed
@fisker fisker deleted the memoize-fs-access branch May 10, 2025 17:06
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.

3 participants