html encode
JavaScript performance comparison
Info
Speed test escaping html characters with if statements and regular expressions.
Preparation code
<script>
Benchmark.prototype.setup = function() {
function encodeWithIfs(str) {
var result = ""
for (var cc = 0; cc < str.length; cc++) {
var c = str.charAt(cc)
if (c == "&") result += '&'
else if (c == '"') result += '"'
else if (c == "'") result += '''
else if (c == "<") result += '<'
else if (c == ">") result += '>'
else result += c
}
return result
}
function endcodeWithRegEx(str) {
return str.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>')
}
function endcodeWithRegEx2(str) {
return str.replace(/[&<>"'`]/g, function (chr) {
return '&#' + chr.charCodeAt(0) + ';'
})
}
function encodeWithSwitchAndArray(str) {
for (var
result = str.split(""),
i = 0, length = result.length;
i < length; i++
) {
switch(result[i]) {
case "&": result[i] = "&"; break;
case '"': result[i] = """; break;
case "'": result[i] = "'"; break;
case "<": result[i] = "<"; break;
case ">": result[i] = ">"; break;
}
}
return result.join("");
}
function encodeWithSwitch(str) {
for (var
c,
result = "",
i = 0, length = str.length;
i < length; i++
) {
switch(c = str.charCodeAt(i)) {
case "&": c = "&"; break;
case '"': c = """; break;
case "'": c = "'"; break;
case "<": c = "<"; break;
case ">": c = ">"; break;
}
result += c;
}
return result;
}
var encodeWithRegExpAndObject = function (re, o) {
function place(m) {
return o[m];
}
return function (str) {
return str.replace(re, place);
};
}(
/[&"'<>]/g,
{"&":"&", '"':""", "'":"'", "<":"<", ">":">"}
);
// NOTE: this isn't very portable; Chrome encodes ", ', < and > but not &;
// Firefox, at least in the past (see https://bugzilla.mozilla.org/649818) // doesn't encode either, other browsers likely are variably better/worse.
// The chaos of the pre-DOM0 days still shows in these vestigial functions
function encodeWithStringPrototype(str) {
return ''.link(str).slice(9,-6).replace(/&/g, '&');
}
function doEncode(func) {
var a = func("<div></div>")
var b = func("This is a string with no replacements")
var c = func("All replacement chars & \" ' < >")
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
ifs |
|
pending… |
regex1 |
|
pending… |
regex2 |
|
pending… |
switch + Array |
|
pending… |
switch |
|
pending… |
RegExp + Object |
|
pending… |
link + slice + regex |
|
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 Dan Rzeppa
- Revision 2: published by Dan Rzeppa
- Revision 3: published by WebReflection
- Revision 4: published by Johan Sundström
- Revision 5: published
- Revision 8: published by Eamon Nerbonne
- Revision 9: published by Eamon Nerbonne
- Revision 10: published by Dan Rzeppa
- Revision 11: published by Dan Rzeppa
0 comments