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

url: reduce deplicated codes in autoEscapeStr #18613

Closed
wants to merge 5 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
Prev Previous commit
Next Next commit
[squash]fix performance regression
  • Loading branch information
starkwang committed Feb 7, 2018
commit ba9427c9555be90122e2ea054218b0562458ce09
38 changes: 26 additions & 12 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,25 +442,39 @@ function validateHostname(self, rest, hostname) {
// Automatically escape all delimiters and unwise characters from RFC 2396.
// Also escape single quotes in case of an XSS attack.
// Return the escaped string.
const escapedCharacters = {
'\t': '%09', '\n': '%0A', '\r': '%0D', ' ': '%20',
'"': '%22', '\'': '%27', '<': '%3C', '>': '%3E',
'\\': '%5C', '^': '%5E', '`': '%60', '{': '%7B',
'|': '%7C', '}': '%7D'
const escapedCharacterCodes = {
9: '%09', 10: '%0A', 13: '%0D', 32: '%20',
34: '%22', 39: '%27', 60: '%3C', 62: '%3E',
92: '%5C', 94: '%5E', 96: '%60', 123: '%7B',
124: '%7C', 125: '%7D'
};
function autoEscapeStr(rest) {
var escaped = '';
var lastEscapedPos = 0;
for (var i = 0; i < rest.length; ++i) {
// Manual switching is faster than using a Map/Object.
Copy link
Member

@joyeecheung joyeecheung Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the whole point of having these switch statements to avoid the overhead of accessing a Map/object? (although that looks quite Crankshaftscript-ish so I doubt if the comment here still holds)...if we are going to access an object anyway, then the switch statements can just be replaced with something like const code = escapedCharacterCodes[rest.charCodeAt(i)]; if (code)..., and this comment can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly what I do in the first commit, but it causes ~10% performance regression in url.parse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmeurer Would you be interested in taking a look at this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intuitively I'd suggest to go with an array instead of an object literal. Did you try with an array?

// `escaped` contains substring up to the last escaped character.
var escapedChar = escapedCharacters[rest[i]];
if (escapedChar) {
// Concat if there are ordinary characters in the middle.
if (i > lastEscapedPos)
escaped += rest.slice(lastEscapedPos, i);
escaped += escapedChar;
lastEscapedPos = i + 1;
var code = rest.charCodeAt(i);
switch (code) {
case 9: // '\t'
case 10: // '\n'
case 13: // '\r'
case 32: // ' '
case 34: // '"'
case 39: // '\''
case 60: // '<'
case 62: // '>'
case 92: // '\\'
case 94: // '^'
case 96: // '`'
case 123: // '{'
case 124: // '|'
case 125: // '}'
// Concat if there are ordinary characters in the middle.
if (i > lastEscapedPos)
escaped += rest.slice(lastEscapedPos, i);
escaped += escapedCharacterCodes[code];
lastEscapedPos = i + 1;
}
}
if (lastEscapedPos === 0) // Nothing has been escaped.
Expand Down