-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
lib: refactor internal/util #11404
lib: refactor internal/util #11404
Conversation
37282b0
to
ece8161
Compare
One thing to keep in mind when inlining functions inside |
Yeah i was thinking about that. Just in general it would be good to move the function bodies out of the object. |
I'm not sure the refactor makes it better, just stylistically different. What is more efficient? There are benchmarks for this? |
modules that do I don't think this kind of sweeping stylistic change is worth doing unless its really compellingly better style or has some compelling functional difference and I'm not sure the "more efficient" style will be noticeable. If we didn't have to backport, I'd have no particular opinion, but sweeping stylistic changes that make backports hard worries me. In contrast, breaking up the absolutely enormous lib/crypto.js is, I think, compellingly better style, easier to read, maintain, etc., so despite the backport cost, I was +1 for that. Here, its not so clear to me. |
@sam-github FWIW: #11430 |
ece8161
to
7ba5f4c
Compare
@mscdex @sam-github ... so this one we will definitely want to investigate further and hold off on landing. Unlike the other similar changes I've done moving to the |
I've added the |
3acfcb3
to
1fe0ac8
Compare
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.
Not sure what is the general practice followed in the node code base. But looks like you've been missing out on semicolons after end of the function block. Looking at the history of the file, function blocks were ended with semicolons.
semicolons are not required to closing function blocks for regular functions with our linting rules. They are only required on assignments. |
* Use the more efficient module.exports = {} approach * Eliminate some uses of arguments
1fe0ac8
to
bf29544
Compare
@mscdex @sam-github ... I've updated this PR. The perf diff with master compared to 7.9 is negligible. The following is the benchmark comparison results for the normalize encoding method in internal/util: Master to 7.9
This PR to 7.9
PTAL |
You can actually remove |
lib/internal/util.js
Outdated
@@ -186,4 +197,27 @@ exports.convertToValidSignal = function convertToValidSignal(signal) { | |||
} | |||
|
|||
throw new Error('Unknown signal: ' + signal); | |||
} | |||
|
|||
module.exports = { |
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.
assigning to exports =
as well is more future proof, it allows util functions to refer to each other more easily.
Of the 3 common export styles, at-top, at-definition (what util used to do), and at-bottom,at-bottom remains my least favorite. It lacks the visibility and readability of export at top, and lacks the cohesion of export at time of definition. But if at-bottom is how node.js goes, I'll follow along.
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.
Avoiding the need to refer to exports.{whatever}
is important and is why the various exported functions are all named top level functions (they can refer to each other without the exports.
bit). That said, it's harmless to add.
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.
One problem with having it at the top is that you can run into variable definition issues, where you're exporting something that gets defined/set later on in the file. Sometimes you might be able to just pull the definition to the top, but other times the assignment may not be a simple one-liner. So putting it at the bottom means you never have to worry about such issues and doing it the same everywhere is good for consistency.
lib/internal/util.js
Outdated
function isError(e) { | ||
return objectToString(e) === '[object Error]' || e instanceof Error; | ||
} | ||
|
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.
sometimes there is two lines beween definitions, sometimes one, should probably be consisten
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.
AFAICT from the diff, this is adding new APIs to util, toInteger()
and toLength()
. Am I misreading?
The |
PR Updated to remove the |
I would prefer for both exports and module.exports to be reassigned, to keep a consistent idiom (and I think it costs nothing). Other than that, LGTM |
lib/internal/util.js
Outdated
return Object.prototype.toString.call(o); | ||
} | ||
|
||
// Mark that a method should not be used. |
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.
These comments shouldn't be indented.
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.
whoops! good catch :-)
Updated! |
LGTM if CI is ok with it: https://ci.nodejs.org/job/node-test-pull-request/7560/ |
Fedora on CI has been a bit challenged today in terms of speed, but everything else looks good. Will get this landed tomorrow assuming there are no issues. |
* Use the more efficient module.exports = {} approach * Eliminate some uses of arguments PR-URL: #11404 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net
Landed in 9077b48 |
@jasnell is this something we want on v6.x? |
Only if there's no negative perf hit. |
Okay, I'm marking this as don't land, but if anyone is willing to backport (and test the perf) on this then go for it. |
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
internal/util