-
-
Notifications
You must be signed in to change notification settings - Fork 12
Reduce fs.access()
call
#99
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
Conversation
[cwd, ...parts].flatMap(part => exeExtensions | ||
.map(extension => access(`${path.resolve(part, file)}${extension}`)), |
There was a problem hiding this comment.
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_); |
There was a problem hiding this comment.
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_);
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's "nano" 😄
There was a problem hiding this comment.
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.
Looks good @sindresorhus? |
When running
test/options.js
, which contains 3 commands:ava
,npm
, andgit
.Before:
fs.access
called 428 times.After:
fs.access
called 210 times.