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
Defer windowWidth() to improve perf for non-help usage #610
Conversation
This. is. awesome. |
@@ -96,11 +96,23 @@ module.exports = function (yargs, y18n) { | |||
epilog = msg | |||
} | |||
|
|||
var wrap = windowWidth() | |||
var wrapSet |
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 would initialize this to false
, for clarity.
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.
agreed, good catch!
Besides my nit-picky inline comments, this LGTM! Thanks @nolanlawson! |
Me likey. 😺 Thanks again, @nolanlawson, this is great! |
@nolanlawson thank you very much for digging into this. |
woot!! Thanks so much @nolanlawson ✨ ✨ |
@nolanlawson @lrlna want to give this a spin?
These performance changes go out with the next release (would love the extra set of eyes). |
@bcoe i am using that on a refactor of one of my projects right now; it's all good so far and I'll keep an eye :D |
Hi 👋, I'm a big fan of yargs, so after talking with @lrlna about #521, I wanted to try my hand at improving the overall runtime of
require('yargs')
.I started by profiling with the Chrome dev tools using
node-nightly
and the--inspect
option, where I noticed thatwindowWidth()
is a very expensive function:Since
windowWidth()
only has to be calculated when we need the column width, e.g. when the user has typed--help
or-h
, I figured we could lazily evaluate it. That's what this PR does.Here is a very small benchmark to demonstrate the perf improvement:
Running in node 5.12.0 on a 2013 Macbook Air, I got the following results (real time, averaged):
Since that includes the cost of the Node process itself, we can also use
performance-now
to measure just the runtime ofrequire('yargs')
:This gives:
So in rough terms
require('yargs')
should be about ~30% faster with this PR, with an average of ~12ms shaved off each invocation ofrequire('yargs')
. (Obviously it doesn't make any difference when doing-h
or--help
, but my hunch is that that's not the average use case.)Please let me know if I missed something in the style/coverage/etc. or if this PR is unfeasible for some other reason I didn't notice. Hope this helps!