This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
At the moment, the
url.parse()
function escapes every character in the list of escapable characters without checking whether it is necessary or not - i.e. it does a lot of work without any reason, leading to performance degradation.The reason I started looking into this was commit 17a379, which lead to a slight performance drop due to the fact that it doubled the list of escapable characters, which is actually a legitimate security fix. However, after digging into it a bit, it became clear that the could actually be improved without affecting security.
My patch simply checks whether the character that needs escaping is in the URL before encoding and replacing it with the safe option. Basically, the only change is represented by this if-statement:
if (rest.indexOf(ae) !== -1)
.On average, this should amount to a ~3x performance increase.
Results
Running the code (inspired by the node benchmarks) above on the current v0.12 HEAD (or any version since approx. v0.4.x) with and without the one-line fix:
Without fix
With fix
Testing
All the unit tests that ship with Node and pass without the fix, pass with the fix as well. Also, by placing a
console.log(parsedLink);
at the end of the microbenchmark above, the output is identical for both versions.