Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

@apapirovski apapirovski Sep 15, 2017

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?

Copy link
Contributor

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.

switch (f.charCodeAt(i + 1)) {
switch (nextChar) {
case 115: // 's'
tempStr = String(arguments[a++]);
break;
Expand All @@ -221,25 +222,19 @@ function format(f) {
tempStr = `${parseFloat(arguments[a++])}`;
break;
case 37: // '%'
i++;
str += f.slice(lastPos, i);
lastPos = i + 1;
continue;
default: // any other character is not a correct placeholder
i++;
continue;
}
if (lastPos !== i)
str += f.slice(lastPos, i);
if (lastPos !== i - 1)
str += f.slice(lastPos, i - 1);
str += tempStr;
i++;
lastPos = i + 1;
} else {
i++;
if (f.charCodeAt(i) === 37) {
str += f.slice(lastPos, i);
lastPos = i + 1;
}
} else if (nextChar === 37) {
str += f.slice(lastPos, i);
lastPos = i + 1;
}
}
}
Expand Down