escape html caching regexp or not.

Benchmark created by ama-ch on


Preparation HTML

<script>
  var html = "<html><body style=\"background: blue;\"><div>'this&is&test.'</div></body></html>";
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
using cache
var ltRe = /</g;
var gtRe = />/g;
var aposRe = /'/g;
var quotRe = /"/g;
var ampRe = /&/g;
for (var i = 0; i < 10000; i++) {
var result = html.replace(ltRe, '&lt;')
                        .replace(gtRe, '&gt;')
                        .replace(aposRe, '&#039;')
                        .replace(quotRe, '&quot;')
                        .replace(ampRe, '&amp;');
}
ready
no cache
for (var i = 0; i < 10000; i++) {
var result = html.replace(/</g, '&lt;')
                        .replace(/>/g, '&gt;')
                        .replace(/'/g, '&#039;')
                        .replace(/"/g, '&quot;')
                        .replace(/&/g, '&amp;');
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.