-
Notifications
You must be signed in to change notification settings - Fork 543
fix: avoid throw for invalid cases when not needed #804
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
08b3d11
to
4c73df0
Compare
classes/semver.js
Outdated
|
||
class SemVer { | ||
constructor (version, options) { | ||
constructor (version, options, throwErrors = true) { |
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.
Should throwErrors
be in 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.
I didn't want to include since this would require me to modify/copy the options object to avoid mutate the original options just to include this flag.
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'm extremely hesitant to add Yet Another Parameter especially in an environment where we already use a kwargs-style 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.
It's ok to mutate the original options object? If so, I can move to the options object.
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 don't think so. Sorry it's a busy week and I'm not really able to intuit why you'd need to mutate the options here. You can try to explain it or take a peek at internal/parse-options.js
to see how we set defaults (i.e. loose=true).
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 right now I'm remembering a bit of this, it's a bit of a funky parser cause what we're supporting is parse('1.2.3', true)
which is kind of a shorthand in semver that the opts can just be defining loose
. It ... was definitely a choice.
I think we can come up with a sensible default here, and it's worth working through so we're not adding a new parameter. We'd really like to encourage just passing an object here.
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 updated the benchmark and moved the noThrow option to the options object.
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.
To be honest, I saw I little perf hit due to the object spread on parse/inc to add the new property but the invalid cases where options by a lot.
So, the question is, how often do we have bad versions?
If you don't think is often, I don't think we should merge this PR with the noThrow in the options via spread.
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.
So, the question is, how often do we have bad versions?
The use case for bad versions is definitely "before it hits a registry", so local package.json files. That's the furthest from a hot path possible. I think the optimization here should be for good data.
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, that's what I thought. So, to be able to implement this change without any slowdowns in the hot path, it would be necessary to add a third parameter on Semver.
Do you think it's worth it? If not, I'll close this PR.
4c73df0
to
029341d
Compare
029341d
to
de78282
Compare
At this point I don't think adding a third parameter is the right way to go, sorry. I do appreciate the work put into this still. |
Hey, how you doing?
I notice some cases where we don't want to throw when instantiating the
semver
, for those cases, I added a property tosemver
instead of throwing an error since this is an expensive operation on node.Old for
bench-parse
:Now:
I'm not sure if this is something you guys are willing to do (adding a new property and have the object partially dead), so l will keep this as draft for now.