Last active
February 10, 2021 00:36
-
-
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
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
<!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> |
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
"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, '&') | |
.replace(/</g, '<') | |
.replace(/>/g, '>') | |
.replace(/"/g, '"'); | |
} | |
const ENTITIES2 = /[<>&"]/g; | |
function replacer(s) { | |
switch (s.charCodeAt()) { | |
case 60: return '<'; break; | |
case 62: return '>'; break; | |
case 34: return '"'; break; | |
case 38: return '&'; break; | |
} | |
} | |
function encodeEntities_replacer(s) { | |
return s.replace(ENTITIES2, replacer); | |
} | |
function replacer2(s) { | |
switch (s) { | |
case '<': return '<'; | |
case '>': return '>'; | |
case '"': return '"'; | |
case '&': return '&'; | |
} | |
} | |
function encodeEntities_replacer2(s) { | |
return s.replace(ENTITIES2, replacer2); | |
} | |
const replacements = { | |
__proto__: null, | |
'<': '<', | |
'>': '>', | |
'"': '"', | |
'&': '&', | |
}; | |
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 = '<'; break; | |
case '>': ch = '>'; break; | |
case '"': ch = '"'; break; | |
case '&': ch = '&'; 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