joyent/node — pull request #8638 by CGavrila (2014-10-28 12:43:53+00:00)
Title: Improve URL parsing speed by 300%

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](https://github.com/joyent/node/commit/17a379ec39a34408477ac6a43751c1b9b2e952a4), 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

```
var ITERATIONS = 100000;

var url = require('url');

var urls = [
'http://nodejs.org/docs/latest/api/url.html#url_url_format_urlobj',
'http://blog.nodejs.org/',
'https://encrypted.google.com/search?q=url&q=site:npmjs.org&hl=en',
'javascript:alert("node is awesome");',
'some.ran/dom/url.thing?oh=yes#whoo'
];

var parsedLink;

for(var i=0; i