-
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
util: improve format performance #15422
Conversation
lib/util.js
Outdated
} | ||
if (lastPos === 0) | ||
str = f; | ||
else if (lastPos < f.length) | ||
str += f.slice(lastPos); | ||
while (a < arguments.length) { | ||
const x = arguments[a++]; | ||
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) { | ||
if (typeof x !== 'object' && typeof x !== 'symbol' || x === null) { |
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 think this is harder to reason about, which is why I always add extra parentheses so it's always immediately clear (especially to newcomers).
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 is interesting to see many people preferring this style. I personally always feel like my brain has to compute a tiny bit more when there are extra braces^^. I changed it back though.
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.
LGTM with the exception of @mscdex's comment
58e8c26
to
d2125d5
Compare
I found a even better way to optimize for unknown types (e.g. if someone uses a percent as a percent). |
lib/util.js
Outdated
@@ -196,8 +196,9 @@ function format(f) { | |||
var lastPos = 0; | |||
for (i = 0; i < f.length - 1; i++) { | |||
if (f.charCodeAt(i) === 37) { // '%' | |||
const nextChar = f.charCodeAt(++i); | |||
if (a !== arguments.length) { |
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.
Would it make sense to store arguments.length
in a const given the extensive usage throughout format
?
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.
Actually, that might be worse for perf after reviewing how V8 handles it. You can ignore, I believe.
Landed in faaefa8 |
PR-URL: #15422 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
PR-URL: #15422 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
PR-URL: nodejs/node#15422 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
PR-URL: nodejs/node#15422 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
should this land in LTS? If so it will need to bake a bit longer. Please change labels as appropriate |
@MylesBorins I think we could still backport this to 8.x but I am not sure if it applies without conflicts or not. Would you be so kind and check that? |
@BridgeAR this already landed on 8.x, has conflicts on 6.x |
Oh, right, I missed that. As 6.x is soon in maintenance only, I do not really think that this has to be backported. I changed the label accordingly. |
thanks @BridgeAR in future please add the |
After working on
util.inspect
I thought it might be worth also checkingutil.format
as that is called byconsole.log
.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
util