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
const replaceByMap0 = (str, replaceMap) => {
return Object.keys(replaceMap).reduce((acc, id) => {
return acc.replace(id, replaceMap[id]);
}, str);
};
const chartsForEscape = /([^a-zA-Z0-9])/g;
const escapeRegExpSyntax = (str) => str.replace(chartsForEscape, '\\$1');
const replaceByMap1 = (str, replaceMap) => {
const regexp = new RegExp(Object.keys(replaceMap)
.map((s) => `(${escapeRegExpSyntax(s)})`)
.join('|'), 'g');
return str.replace(regexp, (str) => replaceMap[str] ?? str);
};
const replaceByMap2 = (str, replaceMap) => {
const indexes = Object.keys(replaceMap)
.map((id) => {
const start = str.indexOf(id);
if (start === -1) {
return undefined;
}
return {
start,
id,
content: replaceMap[id],
};
})
.filter((x) => Boolean(x))
.sort((a, b) => a.start - b.start);
if (!indexes.length) {
return str;
}
return indexes.reduce((acc, { start, id, content }, i, arr) => {
const next = arr[i + 1];
const tailPart = str.substring(start + id.length, next ? next.start : undefined);
return acc + content + tailPart;
}, str.substring(0, indexes[0].start));
};
const replaceByMap3 = (str, replaceMap) => {
const regexp = new RegExp(Object.keys(replaceMap)
.map((s) => `(${escapeRegExpSyntax(s)})`)
.join('|'), 'g');
return str
.split(regexp)
.map((s) => replaceMap[s] ?? s)
.join('');
};
function makeBigStr(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ()/?><';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
const bigStr = makeBigStr(2 * (1 << 20));
const getRandomSubstr = (length, str) => {
const start = Math.floor(Math.random() * str.length - length);
return str.substring(start, start + length);
};
const replaceMap = Object.fromEntries([
[getRandomSubstr(20, bigStr), makeBigStr(5 * (1 << 10))],
[getRandomSubstr(20, bigStr), makeBigStr(5 * (1 << 10))],
[getRandomSubstr(10, bigStr), makeBigStr(4 * (1 << 20))],
[getRandomSubstr(20, bigStr), makeBigStr(5 * (1 << 10))],
]);
Ready to run.
Test | Ops/sec | |
---|---|---|
multiply replace |
| ready |
replace(regexp, () => {}) |
| ready |
multiply indexOf + reduce + substring |
| ready |
split + join |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.