Corrected getParameterByName3

JavaScript performance comparison

Revision 16 of this test case created

Info

getParameterByName3 would bork if other variables on the query string ended in the same text as the name of the one to return, as pointed out by a comment on SO.

Preparation code

<script>
    var location_search = "?q=hol+nse+qu+cirde&nose=%34sdnf%2f&43&l=1"
        , testDebug     = []
    ;

    (function () {
        var location = {};
        location.search = location_search;

        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 regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));

        }

        window.getParameterByName2 = function (name) {
            var match = RegExp('[?&]' + name + '=([^&]*)').exec(location.search);
            return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
        }

        window.getParameterByName3 = function (n) {
            var half = location.search.split(n + '=')[1];
            return half ? decodeURIComponent(half.split('&')[0].replace(/\+/g, ' ')) : null;
        }
       
        window.test_debug = function() {
        }
    })();
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Split method
var qs = window.GetQueryString(location_search.substr(1))
  , search = qs["q"]
  , value = qs["nose"]
  , undef = qs["undefinedstring"]
;

test_debug(1, search,value,undef);
 
pending…
Regex method
var search = window.getParameterByName("q")
  , value = window.getParameterByName("nose")
  , undef = window.getParameterByName("undefinedstring")
;

test_debug(2, search,value,undef);
pending…
Another....
var  search = window.getParameterByName2("q")
  , value = window.getParameterByName2("nose")
  , undef = window.getParameterByName2("undefinedstring")
;

test_debug(3, search,value,undef);
pending…
simplest and faster but inaccurate!
var search = window.getParameterByName3("q")
  , value = window.getParameterByName3("nose")
  , undef = window.getParameterByName3("undefinedstring")
;

test_debug(4, search,value,undef);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment