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 ESCAPED_REGEX = /[<"'&]/
const a =/&/g
const b = /</g
const c = /"/g
const d = /'/g
function s1(str) {
return str.replace(a, '&').replace(b, '<').replace(c, '"').replace(d, ''');
}
function s2(value) {
// This is a optimization to avoid the whole escaping process when the value
// does not contain any special characters.
if (!ESCAPED_REGEX.test(value)) {
return value
}
const length = value.length
let escaped = ''
let start = 0
let end = 0
for (; end < length; end++) {
// https://wonko.com/post/html-escaping
switch (value[end]) {
case '&':
escaped += value.slice(start, end) + '&'
start = end + 1
continue
// We don't need to escape > because it is only used to close tags.
// https://stackoverflow.com/a/9189067
case '<':
escaped += value.slice(start, end) + '<'
start = end + 1
continue
case '"':
escaped += value.slice(start, end) + '"'
start = end + 1
continue
case "'":
escaped += value.slice(start, end) + '''
start = end + 1
continue
}
}
// Appends the remaining string.
escaped += value.slice(start, end)
return escaped
}
function s3(value) {
const length = value.length
let escaped = ''
let start = 0
let end = 0
for (; end < length; end++) {
// https://wonko.com/post/html-escaping
switch (value[end]) {
case '&':
escaped += value.slice(start, end) + '&'
start = end + 1
continue
// We don't need to escape > because it is only used to close tags.
// https://stackoverflow.com/a/9189067
case '<':
escaped += value.slice(start, end) + '<'
start = end + 1
continue
case '"':
escaped += value.slice(start, end) + '"'
start = end + 1
continue
case "'":
escaped += value.slice(start, end) + '''
start = end + 1
continue
}
}
// Appends the remaining string.
escaped += value.slice(start, end)
return escaped
}
Ready to run.
Test | Ops/sec | |
---|---|---|
s1 |
| ready |
s2 |
| ready |
s3 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.