-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
fs: getOption to populate passed in default values #11630
Conversation
In fs.js, the getOption can be improved to populate the option object with the passed in defaultOptions if the default options are not there in the original option object. This way we don't have to do options.flag || 'w' etc.
This PR would be easier to understand if the description contained some example code that does not work as expected before your change (with a description of what you consider expected). |
@sam-github There is nothing that is working unexpectedly. I just felt we are duplicating the work of checking if default values exists or not in the options. For e.g. fs.writeFile = function(file, options, cb) {
options = getOptions(options, {flag: 'w'});
/* later somewhere */
flags = options.flags || 'w'
/* could have just been the below had getOptions populated the default values if not exist */
flags = options.flags
} |
Updated the description to explain what this PR would lead to |
@nodejs/collaborators ... thoughts? |
@@ -56,7 +56,7 @@ function getOptions(options, defaultOptions) { | |||
|
|||
if (options.encoding !== 'buffer') | |||
assertEncoding(options.encoding); | |||
return options; | |||
return Object.assign({}, defaultOptions, options); |
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.
Have you benchmarked this? I thought Object.assign()
was still slow?
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.
never done a benchmark. do we have any existing ones to compare it with? or any suggestions how to do a benchmark? Thanks
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.
I was suggested to use util.extend
, would it be faster ?
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.
Yes, util._extend()
would probably be a better choice, at least in core. Object.assign()
is probably fine for tests and non-runtime stuff.
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.
Related discussion: #7165 (comment)
Why are the tests changed to use |
@rmg I can recall that |
Doh! I should have caught that 😊 |
@rmg it shouldn't be a problem though right ? I mean was there a reason why in the tests the properties were put in the prototype ? |
@fhalde after some digging, it seems those tests are intentionally written to use edit forgot to add, this is actually what the |
@rmg |
@fhalde it seems like this might end up being a lost cause then, since a solution that supports prototypes might end up being more complicated than just leaving things as they are :-( |
Agree too. I'll close it ? @rmg |
@fhalde agreed, may as well close this. FWIW, I liked the idea behind the change. |
Closing based on the discussion. |
In
fs.js
, thegetOption
can be improved to populate the option object with the passed in defaultOptions if the default options are not there in the original option object. This way we don't have to dooptions.flag || 'w'
etc.For example in the
fs.writeFile
Similar patterns are present in few more parts of the
fs
moduleChecklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
fs, test