QueryString with javascript (v25)

Revision 25 of this benchmark created on


Description

Test two methods of retrieving QueryString values with javascript.

Preparation HTML

<script>
  var query = "q=my+search+query&value=55";
  
  window.GetQueryString = function(q) {
   return (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i) {
     var p = a[i].split('=');
     if (p.length != 2) continue;
     b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
   })(q.split("&"));
  };
  
 var pl     = /\+/g,
        search = /([^&=]+)=?([^&]*)/g;

window.GetQueryString = () {
    var match,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);

    var urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
    return urlParams;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Split method
var qs = window.GetQueryString(query);

var search = qs["q"];
var value = qs["value"];
var undef = qs["undefinedstring"];
ready
Regex method
var qs = window.GetQueryString2(query);

var search = qs["q"];
var value = qs["value"];
var undef = qs["undefinedstring"];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.