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 QUOTE_REGEX = /"/
const QUOTE2_REGEX = /"/g
function escapeAttribute(value) {
// Handle non string values
if (typeof value !== 'string') {
// HTML Dates can just be ISO stringified
if (value instanceof Date) {
return value.toISOString()
}
// Calls toString() on the value
value = String(value)
}
// This is a optimization to avoid the whole escaping process when the value
// does not contain any double quotes
if (!QUOTE_REGEX.test(value)) {
return value
}
const length = value.length
let escaped = ''
let start = 0
let end = 0
for (; end < length; end++) {
switch (value[end]) {
case '"':
escaped += value.slice(start, end) + '"'
start = end + 1
continue
}
}
// Appends the remaining string.
escaped += value.slice(start, end)
return escaped
}
function escapeAttribute2(value) {
// Handle non string values
if (typeof value !== 'string') {
// HTML Dates can just be ISO stringified
if (value instanceof Date) {
return value.toISOString()
}
// Calls toString() on the value
value = String(value)
}
return value.replace(QUOTE2_REGEX, '"')
}
function escapeAttribute3(value) {
// Handle non string values
if (typeof value !== 'string') {
// HTML Dates can just be ISO stringified
if (value instanceof Date) {
return value.toISOString()
}
// Calls toString() on the value
value = String(value)
}
const length = value.length
let escaped = ''
let start = 0
let end = 0
for (; end < length; end++) {
switch (value[end]) {
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 | |
---|---|---|
1 |
| ready |
2 |
| ready |
3 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.