String.link HTML Escapement
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var div = document.createElement('div');
var properValue = "foo<>'"";
var value;
function stringDotLink(str) {
return String.prototype.link(str).split('"')[1];
}
function divTextContent(str) {
div.textContent = "foo<>'\"";
return div.innerHTML.replace(/'/g,''').replace(/"/g,'"');
}
function naiveReplacer(s) {
if (s==='<') {
return '<';
}
if (s==='>') {
return '>';
}
if (s==="'") {
return ''';
}
if (s==='"') {
return '"';
}
}
var map={ '<':'<', '>':'>', "'":''', '"':'"' };
function mapReplacer(s) {
return map[s] || s;
}
function regexpSingleNaive(str) {
return str.replace(/[<>'"]/g, naiveReplacer);
}
function regexpSingleMap(str) {
return str.replace(/[<>'"]/g, naiveReplacer);
}
function regexpMultiple(str) {
return (str.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/'/g, ''')
.replace(/"/g, '"'));
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
String.prototype.link() |
|
pending… |
innerText<>innerHTML |
|
pending… |
RegExp (single, naive) |
|
pending… |
RegExp (single, map) |
|
pending… |
RegExp (multiple) |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Jason Miller
- Revision 2: published by Jason Miller
- Revision 3: published by Jason Miller
0 comments