Skip to content

Commit

Permalink
Add fast path for simple URL parsing
Browse files Browse the repository at this point in the history
This patch adds a fast path for parsing of simple path-only URLs, as commonly
found in HTTP requests received by a server.

Benchmark results [ms], before / after patch:
/foo/bar              0.008956   0.000418 (fast path used)
http://example.com/   0.011426   0.011437 (normal slow path, no change)

In a simple 'ab' benchmark of a single-threaded web server, this patch
increases the request rate from around 6400 to 7400 req/s.

Reviewed-By: Fedor Indutny <[email protected]>
  • Loading branch information
gwicke authored and indutny committed Jul 31, 2014
1 parent ff6117b commit 4b59db0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function Url() {
var protocolPattern = /^([a-z0-9.+-]+:)/i,
portPattern = /:[0-9]*$/,

// Special case for a simple path URL
simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/,

// RFC 2396: characters reserved for delimiting URLs.
// We actually just auto-escape these.
delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'],
Expand Down Expand Up @@ -119,6 +122,25 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
// This is to support parse stuff like " http://foo.com \n"
rest = rest.trim();

if (!slashesDenoteHost && hashSplit.length === 1) {
// Try fast path regexp
var simplePath = simplePathPattern.exec(rest);
if (simplePath) {
this.path = rest;
this.href = rest;
this.pathname = simplePath[1];
if (simplePath[2]) {
this.search = simplePath[2];
if (parseQueryString) {
this.query = querystring.parse(this.search);
} else {
this.query = this.search.substr(1);
}
}
return this;
}
}

var proto = protocolPattern.exec(rest);
if (proto) {
proto = proto[0];
Expand Down

0 comments on commit 4b59db0

Please sign in to comment.