Corrected getParameterByName3
JavaScript performance comparison
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 query = "?pdq=wrong+search+value&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, " "));
}
window.getParameterByName2 = function(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.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]):null;
}
window.getParameterByName4 = function(n){
var half = location.search.split('&'+n+'=')[1];
if (!half) half = location.search.split('?'+n+'=')[1];
return half? decodeURIComponent(half.split('&')[0]):null;
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Split method |
|
pending… |
Regex method |
|
pending… |
Another.... |
|
pending… |
simplest and faster but inaccurate! |
|
pending… |
Corrected "simplest" but slower! |
|
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:
- Revision 1: published by BrunoLM
- Revision 2: published by Andrew Petersen
- Revision 3: published by Andy E
- Revision 4: published
- Revision 5: published
- Revision 8: published by Justin
- Revision 9: published by Martin
- Revision 10: published by rufwork
- Revision 12: published by amin
0 comments