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 performance of different implement
<script>
function getQueryParam1(name, url, defaultValue) {
let index = url.indexOf(name);
if (index < 0 || url.charAt(index + name.length) !== '=') {
if (defaultValue !== undefined) {
return defaultValue;
}
return '';
}
index += name.length + 1;
let end;
const endChar1 = url.indexOf('&', index);
const endChar2 = url.indexOf('#', index);
if (endChar1 < 0 && endChar2 < 0) {
end = url.length;
} else if (endChar1 > 0 && endChar2 > 0) {
end = Math.min(endChar1, endChar2);
} else {
end = Math.max(endChar1, endChar2);
}
if (end === index) {
if (defaultValue !== undefined) {
return defaultValue;
}
return '';
}
let char = url.substring(index, end);
if (char === 'undefined') {
return '';
}
if (char.indexOf('+') >= 0) {
char = char.replace(/\+/g, ' ');
}
return decodeURIComponent(char);
}
function getQueryParam2(name, url = '', defaultValue) {
name = name.replace(/[\[\]]/g, "\\$&"); // eslint-disable-line
const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`);
let results = regex.exec(url); // eslint-disable-line
if (!results || !results[2]) {
if (defaultValue !== undefined) {
return defaultValue;
}
return '';
}
results[2] = results[2].replace(/\+/g, " ");
if (results[2] === 'undefined') {
results[2] = '';
}
return decodeURIComponent(results[2]);
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
implement of charAt |
| ready |
implement of RegExp |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.