Last active
July 24, 2022 06:29
-
-
Save intrnl/07816a7669d0ad4620390bd69e59b9d4 to your computer and use it in GitHub Desktop.
HTML escape function #bench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { run, bench, group } from 'https://esm.sh/mitata'; | |
function escape (value) { | |
const str = '' + value; | |
const res = str.replace(/[&<]/g, (char) => '&#' + char.charCodeAt(0) + ';'); | |
return res; | |
} | |
const ATTR_REGEX = /[&"]/g; | |
const TEXT_REGEX = /[&<]/g; | |
function escape_new (value, is_attr = false) { | |
const str = '' + value; | |
const pattern = is_attr ? ATTR_REGEX : TEXT_REGEX; | |
pattern.lastIndex = 0; | |
let escaped = ''; | |
let last = 0; | |
while (pattern.exec(str)) { | |
const idx = pattern.lastIndex - 1; | |
escaped += str.substring(last, idx) + ('&#' + str.charCodeAt(idx) + ';'); | |
last = idx + 1; | |
} | |
return escaped + str.substring(last); | |
} | |
function escape_no_re (value, is_attr = false) { | |
const str = '' + value; | |
let escaped = ''; | |
let last = 0; | |
for (let idx = 0, len = str.length; idx < len; idx++) { | |
const char = str.charCodeAt(idx); | |
if (char === 38 || char === (is_attr ? 34 : 60)) { | |
escaped += str.substring(last, idx) + ('&#' + char + ';'); | |
last = idx + 1; | |
} | |
} | |
if (last === 0) { | |
return str; | |
} | |
return escaped + str.substring(last); | |
} | |
group('no escaping - long', () => { | |
bench('escape', () => { | |
escape('a quick brown fox jumps over the lazy dog', false); | |
}); | |
bench('escape_new', () => { | |
escape_new('a quick brown fox jumps over the lazy dog', false); | |
}); | |
bench('escape_no_re', () => { | |
escape_no_re('a quick brown fox jumps over the lazy dog', false); | |
}); | |
}); | |
group('no escaping - short', () => { | |
bench('escape', () => { | |
escape('brown fox', false); | |
}); | |
bench('escape_new', () => { | |
escape_new('brown fox', false); | |
}); | |
bench('escape_no_re', () => { | |
escape_no_re('brown fox', false); | |
}); | |
}); | |
group('need escaping - long', () => { | |
bench('escape', () => { | |
escape('a quick brown fox fooo<=bar&baz; jumps over the lazy dog', false); | |
}); | |
bench('escape_new', () => { | |
escape_new('a quick brown fox fooo<=bar&baz; jumps over the lazy dog', false); | |
}); | |
bench('escape_no_re', () => { | |
escape_no_re('a quick brown fox fooo<=bar&baz; jumps over the lazy dog', false); | |
}); | |
}); | |
group('need escaping - short', () => { | |
bench('escape', () => { | |
escape('brown < fox', false); | |
}); | |
bench('escape_new', () => { | |
escape_new('brown < fox', false); | |
}); | |
bench('escape_no_re', () => { | |
escape_no_re('brown < fox', false); | |
}); | |
}); | |
await run(); |
Author
intrnl
commented
Jul 24, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment