html escaping via dom
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
// This is a reduced and incomplete split fix
// It only fixes the improper handling of simple capturing groups in IE<=8
(function() {
// Only patch implementation who do not properly handle capturing groups
if ('x'.split(/(x)/).length != 3) {
var nativeSplit = String.prototype.split;
String.prototype.split = function(str, separator, limit) {
// Call native split for non-regexp separator
if (Object.prototype.toString.call(separator) !== "[object RegExp]")
return nativeSplit.call(str, separator, limit);
var output = [];
var match = separator.exec(str);
var matchStart = 0;
limit = (limit === undefined ? -1 : limit) >>> 0;
while (match) {
var nextMatchStart = match.index + match[0].length;
if (nextMatchStart > matchStart) {
output.push(str.slice(matchStart, match.index));
if (match.length > 1 && match.index < str.length)
Array.prototype.push.apply(output, match.slice(1));
str = nextMatchStart;
if (output.length > limit)
break;
}
match = separator.exec(str);
}
return output;
};
}
})();
function coerceToString(val) {
return String((val === null || val === undefined) ? '' : val);
}
var rAmp = /&/g,
rLt = /</g,
rGt = />/g,
rApos = /\'/g,
rQuot = /\"/g,
hChars = /[&<>\"\']/;
var dom_escape = (function() {
var el = document.createElement('b');
if (el.textContent) {return (function(str) {
return hChars.test(str) ? (el.textContent = coerceToString(str), el.innerHTML) : str;
})} else {return (function(str) {
return hChars.test(str) ? (el.innerText = coerceToString(str), el.innerHTML) : str;
})}
})();
var hogan_escape = (function() {
return (function(str) {
str = coerceToString(str);
return hChars.test(str) ? str.replace(rAmp, '&').replace(rLt, '<').replace(rGt, '>').replace(rApos, ''').replace(rQuot, '"') : str;
});
})();
var split_escape = function(str) {
var input = coerceToString(str).split(/([&<>\"\'])/g);
var inputLengthM1 = input.length-1;
var output = [];
for (var i = 0; i < inputLengthM1; i += 2) {
var c = input[i+1];
output.push(input[i]);
output.push(
c == "'" ? ''' :
c == '"' ? '"' :
c == '&' ? '&' :
c == '<' ? '<' : '>');
}
output.push(input[inputLengthM1]);
return output.join('');
}
var split_concat_escape = function(str) {
var input = coerceToString(str).split(/([&<>\"\'])/g);
var inputLengthM1 = input.length-1;
var output = input[inputLengthM1];
for (var i = 0; i < inputLengthM1; i += 2) {
var c = input[i+1];
output = input[i] +
(
c == "'" ? ''' :
c == '"' ? '"' :
c == '&' ? '&' :
c == '<' ? '<' : '>') + output;
}
return output;
}
var roseta = {
"'" : ''',
'"' : '"',
'&' : '&',
'<' : '<' ,
'>' : '>'};
var split_concat_hash_escape = function(str) {
var input = coerceToString(str).split(/([&<>\"\'])/g);
var inputLengthM1 = input.length-1;
var output = input[inputLengthM1];
for (var i = 0; i < inputLengthM1; i += 2)
output = input[i] + roseta[input[i+1]] + output;
return output;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
dom |
|
pending… |
hogan |
|
pending… |
split join |
|
pending… |
split concat |
|
pending… |
split concat hash |
|
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 kmichel
- Revision 2: published by kmichel
0 comments