QueryString with javascript (v4)

Revision 4 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("&"));
  };

  window.getParameterByName = function(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return decodeURIComponent(results[1].replace(/\+/g, " "));
  }
</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 search = window.getParameterByName("q");
var value = window.getParameterByName("value");
var undef = window.getParameterByName("undefinedstring");
ready

Revisions

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