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
function parseURL(exampleUrl) {
const params = {};
let host;
let port = 80;
let pathname;
let protocol;
let query;
return (url) => {
let pointer = 0;
let prev;
let pprev;
let i = 0;
while (i < url.length) {
const char = url.charAt(i);
if (pointer === 0 && char === '/' && prev === '/' && pprev === ':') {
protocol = url.substring(0, i - 2);
pointer = i;
} else if (pointer > 0 && protocol && !host && char === '/') {
host = url.substring(pointer + 1, i);
pointer = i;
} else if (pointer > 0 && protocol && !host && char === ':') {
host = url.substring(pointer + 1, i);
pointer = i;
} else if (pointer > 0 && protocol && host && char === '/') {
port = (url.substring(pointer + 1, i));
pointer = i;
} else if (pointer > 0 && protocol && host && port) {
pathname = url.substring(pointer + 1, i + 1);
} else if (pointer > 0 && protocol && host && port && pathname) {
query = '';
}
pprev = prev;
prev = char;
i++
}
return {
href: url,
origin: `${protocol}://${host}:${port}`,
protocol,
host: `${host}:${port}`,
hostname: host,
port,
pathname
}
}
}
const prepareMatches = (match) => {
let i = 0;
let lastIndex = -1;
const map = [];
let currentPath;
let char;
while ((i = match.indexOf('/', lastIndex)) !== -1) {
currentPath = match.substring(lastIndex, i);
char = currentPath.charAt(0);
if (char === ':') {
map.push(currentPath.substr(1));
} else {
map.push(null);
}
lastIndex = i + 1;
}
currentPath = match.substring(lastIndex);
char = currentPath.charAt(0);
if (char === ':') {
map.push(currentPath.substr(1));
} else {
map.push(null);
}
return map;
};
const fastPathParse = (match) => {
const map = prepareMatches(match);
// eslint-disable-next-line complexity
return (path) => {
let params = null;
let i;
let lastIndex = -1;
let index = 0;
let key;
let value;
while ((i = path.indexOf('/', lastIndex)) !== -1) {
value = path.substring(lastIndex, i);
key = map[index];
if (key) {
if (!params) {
params = {};
}
params[key] = value;
}
lastIndex = i + 1;
index += 1;
}
value = path.substring(lastIndex);
key = map[index];
if (key) {
if (!params) {
params = {};
}
params[key] = value;
}
return params;
};
};
const URL1 = parseURL(`/users/:id/settings`);
const URL2 = fastPathParse(`/users/:id/settings`);
Ready to run.
Test | Ops/sec | |
---|---|---|
own-impl |
| ready |
url |
| ready |
fast-path-parse |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.