QueryString with javascript
JavaScript performance comparison
Info
Test two methods of retrieving QueryString values with javascript. This is a little fairer than BrunoLM's original jsperf test as those two methods attack the problem in different ways.
My code parses the entire query string in one go like BrunoLM's.
Preparation code
<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
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Split |
|
pending… |
Regex |
|
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