QueryString with javascript (v3)

Revision 3 of this benchmark created by Andy E on


Preparation HTML

<script>
  var query = "q=my+search+query&value=55";
  
  getQSSplit = 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[decodeURIComponent(p[0].replace(/\+/g, " "))] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
   })(q.split("&"));
  };
  
  var urlParams = {};
  function getQSRegEx () {
      var e,
          a = /\+/g,  // Regex for replacing addition symbol with a space
          r = /([^&=]+)=?([^&]*)/g,
          d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
          q = query;
  
      while (e = r.exec(q))
         urlParams[d(e[1])] = d(e[2]);
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Split
var qs = getQSSplit(query);
ready
Regex
getQSRegEx()
ready

Revisions

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