summaryrefslogtreecommitdiff
path: root/lib/url.js
diff options
context:
space:
mode:
authorGabriel Wicke <wicke@wikidev.net>2014-07-01 12:28:49 -0700
committerFedor Indutny <fedor@indutny.com>2014-07-31 22:56:46 +0400
commit4b59db008cec1bfcca2783f4b27c630c9c3fdd73 (patch)
treeb2b5751de9a8d2240f5e715dd7c847ae6b07b31e /lib/url.js
parentff6117b8ed57089001bdc3659b79d23bea1107fe (diff)
downloadandroid-node-v8-4b59db008cec1bfcca2783f4b27c630c9c3fdd73.tar.gz
android-node-v8-4b59db008cec1bfcca2783f4b27c630c9c3fdd73.tar.bz2
android-node-v8-4b59db008cec1bfcca2783f4b27c630c9c3fdd73.zip
Add fast path for simple URL parsing
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 <fedor@indutny.com>
Diffstat (limited to 'lib/url.js')
-rw-r--r--lib/url.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/url.js b/lib/url.js
index 292ce3344b..f0be09c52d 100644
--- a/lib/url.js
+++ b/lib/url.js
@@ -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'],
@@ -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];