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
This is a performance test between two variations on make a string plain text.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
var testData = '<This> is a test & string ".';
var checkPlain1 = function(str) {
var character, regex, replace = {
'&': '&',
'"': '"',
'<': '<',
'>': '>'
};
str = String(str);
for (character in replace) {
if (replace.hasOwnProperty(character)) {
regex = new RegExp(character, 'g');
str = str.replace(regex, replace[character]);
}
}
return str;
};
var checkPlain2 = function(str) {
var length, r = new RegExp('(&)|(\")|(<)|(>)|([^&\"<>]*)', 'g'),
replacements = ["&", """, "<", ">"],
retval = '',
match = r.exec(str);
while (match[0] != "") {
length = match.length;
for (var i = 1; i < length; ++i) {
if (match[i] != undefined) {
retval += (i == 5 ? match[i] : replacements[i - 1]);
}
}
match = r.exec(str);
}
return retval;
};
var checkPlain3 = function(str) {
var r = new RegExp('(&)|(\")|(<)|(>)|([^&\"<>]*)', 'g'),
replacements = ["&", """, "<", ">"],
retval = '',
match = r.exec(str);
while (match[0] != "") {
for (var i = 1; i < match.length; ++i) {
if (match[i] != undefined) {
retval += (i == 5 ? match[i] : replacements[i - 1]);
}
}
match = r.exec(str);
}
return retval;
};
function checkPlain4(str) {
var replace = {
'&': '&',
'"': '"',
'<': '<',
'>': '>'
},
chars = str.split(''),
len = chars.length,
out = '';
for (var i = 0; i < len; ++i) {
if (replace[chars[i]] != undefined) {
out += replace[chars[i]];
}
else {
out += chars[i];
}
}
return out;
};
function checkPlain5(str) {
var replace = {
'&': '&',
'"': '"',
'<': '<',
'>': '>'
},
len = str.length,
out = '';
for (var i = 0; i < len; ++i) {
if (replace[str[i]] != undefined) {
out += replace[str[i]];
}
else {
out += str[i];
}
}
return out;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Multi Pass |
| ready |
Single Pass |
| ready |
Single Pass Take 2 |
| ready |
No regexp |
| ready |
No reg or split |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.