jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Test two methods of retrieving QueryString values with javascript.
<script>
var query = "q=my+search+query&value=7&foo=&bar=hello&bar=world#fragment";
window.getParams1 = 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("&"));
};
var pl = /\+/g,
search = /([^&=]+)=?([^&]*)/g;
window.getParams2 = function() {
var match,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); };
var urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
return urlParams;
};
window.getParams3 = function(qsRAW) {
if(!qsRAW) return {};
var qs = {};
var qsLen = qsRAW.length;
var curKey = "";
var curVal = "";
var compilingKey = true;
for(var i = 0; i < qsLen; i++) {
var char = qsRAW[i];
//If we've hit an equal sign, and we're not on the first character, then
//we should stop compiling our key string, and should move to the val string
if(char === "=" && i !== 0 && compilingKey === true) {
compilingKey = false;
//We've hit an &, and we're in value compile mode, or we're at the last character --> Store the curVal into curKey.
} else if(char === "&" || char === ";" || i === qsLen-1) {
var decodedCurVal = decodeURIComponent(curVal);
//If the key has [] as the last two chars, cut them off. We're dealing with an array of values.
if(curKey.slice(-2) === "[]") curKey = curKey.slice(0,-2);
//If we already have a value for this key, turn it into an Array and push the next value into it.
if(typeof qs[curKey] !== "undefined") {
if(typeof qs[curKey] === "string") {
var tmpVal = qs[curKey];
qs[curKey] = new Array();
qs[curKey].push(tmpVal);
};
qs[curKey].push(decodedCurVal);
//First time we're seeing this key, just assign the value to it
} else {
qs[curKey] = decodedCurVal;
}
curKey = "";
curVal = "";
compilingKey = true;
} else {
if(compilingKey) {
curKey += char;
} else {
curVal += char;
}
}
}
return qs;
};
window.getParams4 = function (s) {
var obj = {}, isKey = true, key = '', val = '', chr;
for(var i=0, l=s.length; i<l; i++){
chr = s[i];
if(chr === '=') {
isKey = false;
continue;
} else if(chr === '&') {
isKey = true;
obj[key] = decodeURIComponent(val);
key = '';
val = '';
continue;
} else if(chr === '#') {
break;
} else if(chr === '?') {
continue;
}
if(isKey){
key += chr;
} else {
val += chr;
}
}
if(key !== ''){
obj[key] = decodeURIComponent(val);
}
return obj;
};
function getQueryString(url) {
var qs = {};
qs = (function(a) {
var b = {},
c;
for (var i = 0; i < a.length; i++) {
c = a[i].split("=");
b[decodeURIComponent(c[0])] = decodeURIComponent(c[1]);
}
return b;
}) (url.split("&"));
return qs;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Split method |
| ready |
Regex method |
| ready |
Advanced (but has bugs) String Scan Method |
| ready |
Simple String Scan Method |
| ready |
My test |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.