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
<script>
function safe_tags_regex(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
function encodeByCharCodeAppend(str) {
var result = "", len = str.length, doneUpto = 0;
for (var i=0; i < len ; i++) {
var ref, code = str.charCodeAt(i);
if(code > 62) continue; //fast-path
else if(code === 38) ref = "&";
else if(code === 34) ref = """;
else if(code === 39) ref = "'";
else if(code === 60) ref = "<";
else if(code === 62) ref = ">";
else continue;
result += doneUpto === i ? ref : str.substring(doneUpto, i) + ref;
doneUpto = i + 1;
}
if(doneUpto !== len) result += str.substring(doneUpto, len);
return result;
}
function safe_tags_dom(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
var safe_tags_dom_cached = (function(){
var div = document.createElement('div');
return function(str) {
div.textContent = str;
return div.innerHTML;
}
})();
function safe_tags_dom_textcontent(str) {
var div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
var safe_tags_textarea_cached = (function(){
var textarea = document.createElement('textarea');
return function (str) {
textarea.textContent=str;
return textarea.innerHTML;
};
})();
var safe_tags_replace = (function(){
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
return function(str) {
return str.replace(/[&<>]/g, function (tag) {
return tagsToReplace[tag];
});
}
})();
var test;//global to prevent easy dead-code-elimination.
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Using Regular Expression |
| ready |
Using replace |
| ready |
Using textarea |
| ready |
Using DOM textNode |
| ready |
Using DOM textContent |
| ready |
Using DOM (cached node) |
| ready |
Using plain loop |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.