Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Last active February 10, 2021 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jridgewell/62ffe7cc62fe8913ef4c89991e034169 to your computer and use it in GitHub Desktop.
Save jridgewell/62ffe7cc62fe8913ef4c89991e034169 to your computer and use it in GitHub Desktop.
HTML Entity Encoding / Escapement #jsbench #jsperf (https://jsbench.github.io/#62ffe7cc62fe8913ef4c89991e034169) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>HTML Entity Encoding / Escapement #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
const STRINGS = [
'foo',
'bar',
'',
'<',
'>',
'&',
'"',
'<div>',
'<span class="foo">A&B</span>',
'hello, world!',
'The movie was called "Jumanji".',
'What\'s up?',
'carol & bob',
'if (i > 20) {}',
'if (foo < 20 && bar > foo) { return "hello"; }'
];
for (let s='', l=STRINGS.length, i=0; i<l; i++) STRINGS.push(s+=STRINGS[i]);
for (let s='', i=STRINGS.length; i--; ) STRINGS.push(s+=STRINGS[i]);
function encodeEntities_replace(s) {
return s
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
const ENTITIES2 = /[<>&"]/g;
function replacer(s) {
switch (s.charCodeAt()) {
case 60: return '&lt;'; break;
case 62: return '&gt;'; break;
case 34: return '&quot;'; break;
case 38: return '&amp;'; break;
}
}
function encodeEntities_replacer(s) {
return s.replace(ENTITIES2, replacer);
}
function replacer2(s) {
switch (s) {
case '<': return '&lt;';
case '>': return '&gt;';
case '"': return '&quot;';
case '&': return '&amp;';
}
}
function encodeEntities_replacer2(s) {
return s.replace(ENTITIES2, replacer2);
}
const replacements = {
__proto__: null,
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'&': '&amp;',
};
function replacer3(s) {
return replacements[s];
}
function encodeEntities_replacer3(s) {
return s.replace(ENTITIES2, replacer3);
}
function encodeEntities_scanstr(s) {
let out = '';
for (let i=0; i<s.length; i++) {
let ch = s[i];
switch (ch) {
case '<': ch = '&lt;'; break;
case '>': ch = '&gt;'; break;
case '"': ch = '&quot;'; break;
case '&': ch = '&amp;'; break;
}
out += ch;
}
return out;
}
};
suite.add("replace", function () {
// replace
for (const str of STRINGS) encodeEntities_replace(str);
});
suite.add("scan (str)", function () {
// scan (str)
for (const str of STRINGS) encodeEntities_scanstr(str);
});
suite.add("replacer", function () {
// replacer
for (const str of STRINGS) encodeEntities_replacer(str);
});
suite.add("replacer2", function () {
// replacer2
for (const str of STRINGS) encodeEntities_replacer2(str);
});
suite.add("replacer3", function () {
// replacer3
for (const str of STRINGS) encodeEntities_replacer3(str);
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("HTML Entity Encoding / Escapement #jsbench #jsperf");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment