This repository was archived by the owner on Apr 22, 2023. It is now read-only.
Improve URL parsing speed by 300%#8638
Closed
CGavrila wants to merge 1 commit intonodejs:masterfrom
CGavrila:master
Closed
Improve URL parsing speed by 300%#8638CGavrila wants to merge 1 commit intonodejs:masterfrom CGavrila:master
CGavrila wants to merge 1 commit intonodejs:masterfrom
CGavrila:master
Conversation
The url.parse() function now checks whether an escapable character is in the URL before trying to escape it.
bnoordhuis
pushed a commit
to bnoordhuis/io.js
that referenced
this pull request
Dec 6, 2014
The url.parse() function now checks whether an escapable character is in the URL before trying to escape it. PR-URL: nodejs/node-v0.x-archive#8638 Reviewed-By: Ben Noordhuis <[email protected]>
bnoordhuis
added a commit
to bnoordhuis/io.js
that referenced
this pull request
Dec 6, 2014
Based on the ad-hoc benchmark from nodejs/node-v0.x-archive#8638.
bnoordhuis
added a commit
to nodejs/node
that referenced
this pull request
Dec 9, 2014
Based on the ad-hoc benchmark from nodejs/node-v0.x-archive#8638 plus an additional benchmark for user:pass auth URLs. PR-URL: #102 Reviewed-by: Chris Dickinson <[email protected]>
trevnorris
pushed a commit
that referenced
this pull request
Dec 30, 2014
The url.parse() function now checks whether an escapable character is in the URL before trying to escape it. PR-URL: #8638 [[email protected]: Switch to use continue instead of if] Signed-off-by: Trevor Norris <[email protected]>
|
Thanks. Landed in 6a03fce. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.