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
Simply do not create a new dom node each time just to inject innerHtml, this is just like a function since it is proxying the call to innerText.
<script>
function safe_tags_regex(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
function safe_tags_dom(str) {
if (window.encodingDiv === undefined) {
window.encodingDiv = document.createElement('div');
}
var div = window.encodingDiv;
var n = document.createTextNode(str);
div.appendChild(n);
var result = div.innerHTML;
div.removeChild(n);
return result;
}
function safe_tags_dom2(str) {
if (window.encodingDiv === undefined) {
window.encodingDiv = document.createElement('div');
}
var div = window.encodingDiv;
div.innerText = str;
return div.innerHTML;
}
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
function safe_tags_replace(str) {
return str.replace(/[&<>]/g, replaceTag);
}
function split_join(str) {
return str.split('&').join('&')
.split('<').join('<')
.split('>').join('>');
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Using Regular Expression |
| ready |
Using cached DOM textNode |
| ready |
Using replace |
| ready |
Using cached DOM innerText |
| ready |
Using split/join |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.