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
Add a test using split and join, like the encode function in jQuery Templates.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
var html = document.body.innerHTML;
var map = {
"&": "&",
"'": "'",
'"': """,
"<": "<",
">": ">"
};
var tempJqElement = $(document.createElement("div"));
var tempElement = document.createElement("div");
function replace_with_switch(c) {
switch (c) {
case "&":
return "&";
case "'":
return "'";
case '"':
return """;
case "<":
return "<";
case ">":
return ">";
}
}
function replace_with_map(c) {
return map[c];
}
function replace_charcode(c) {
return '&#' + c.charCodeAt(0) + ';';
}
var re_all = /[&"'<>]/g;
var re_amp = /&/g;
var re_quot = /"/g;
var re_apos = /'/g;
var re_lt = /</g;
var re_gt = />/g;
var re_arr = [
[re_amp, '&'],
[re_quot, '"'],
[re_apos, '''],
[re_lt, '<'],
[re_gt, '>']
];
function encodeCustom(s) {
return s.replace(/['"<>&]/g, function(s) {
switch (s) {
case "'":
return "'";
case '"':
return """;
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
}
});
}
Ready to run.
Test | Ops/sec | |
---|---|---|
multiple replace() |
| ready |
single replace with map |
| ready |
single replace with switch |
| ready |
createTextNode |
| ready |
use jQuery with pre-created element |
| ready |
using textContent/innerText with precreated element |
| ready |
new Option |
| ready |
createTextNode with precreated element |
| ready |
multiple replace using array |
| ready |
convert to charCode entities |
| ready |
Split and join |
| ready |
.replace |
| ready |
jquery |
| ready |
custom |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.