Encode HTML entities (safe_tags_replace with cached regex) (v53)

Revision 53 of this benchmark created on


Description

compare "".replace(/[&<>]/g ... against "".replace(_cachedRegex ...

Preparation HTML

<script>
 
  var tagsToReplace = {
   '&': '&amp;',
   '<': '&lt;',
   '>': '&gt;'
  };
  var _regex = /[&<>]/g;

  function replaceTag(tag) {
   return tagsToReplace[tag] || tag;
  }
  
  function safe_tags_replace(str) {
   return str.replace(_regex, replaceTag);
  }
  
  function safe_tags_replace2(str) {
   return str.replace(/[&<>]/g, replaceTag);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Using replace
var test = safe_tags_replace('if (a < b && c > d) {} // Héllö naõ');
ready
Using replace (cached regex)
var test = safe_tags_replace2('if (a < b && c > d) {} // Héllö naõ');
ready

Revisions

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