From 5166758927e7538d413b030d3b586107cd53024d Mon Sep 17 00:00:00 2001 From: Philip Tellis Date: Sat, 3 Dec 2011 00:12:32 +0530 Subject: [PATCH] Make QueryString.parse run faster Use decodeURIComponent when appropriate, and only fall back to querystring.decode if it throws, or if the character is a '+'. Fix #2248 --- lib/querystring.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/querystring.js b/lib/querystring.js index 58b90250c44..d709c07fdf5 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -170,9 +170,17 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq) { } qs.split(sep).forEach(function(kvp) { - var x = kvp.split(eq); - var k = QueryString.unescape(x[0], true); - var v = QueryString.unescape(x.slice(1).join(eq), true); + var x = kvp.split(eq), k, v, useQS=false; + try { + if (kvp.match(/\+/)) { // decodeURIComponent does not decode + to space + throw "has +"; + } + k = decodeURIComponent(x[0]); + v = decodeURIComponent(x.slice(1).join(eq) || ""); + } catch(e) { + k = QueryString.unescape(x[0], true); + v = QueryString.unescape(x.slice(1).join(eq), true); + } if (!hasOwnProperty(obj, k)) { obj[k] = v;