Encode HTML entities (v64)

Revision 64 of this benchmark created on


Preparation HTML

<script>
  function safe_tags_regex(str) {
   return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  }
  
  function safe_tags_dom(str) {
   var div = document.createElement('div');
   div.appendChild(document.createTextNode(str));
   return div.innerHTML;
  }
  
  function safe_tags_dom2(str) {
   var div = document.createElement('div');
   div.innerText = str;
   return div.innerHTML;
  }
  
  var tagsToReplace = {
   '&': '&amp;',
   '<': '&lt;',
   '>': '&gt;'
  };
  
  function replaceTag(tag) {
   return tagsToReplace[tag] || tag;
  }
  
  function safe_tags_replace(str) {
   return str.replace(/[&<>]/g, replaceTag);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Using Regular Expression
var test = safe_tags_regex('if (a < b && c > d) {} // Héllö naõn the context of a revision list, A...B is how git-rev-parse defines it. git-log takes a revision list. git-diff does not take a list of revisions - it takes one or two revisions, and has defined the A...B syntax to mean how its defined in the git-diff manpage. If git-diff did not explicitly define A...B, then that syntax would be invalid. Note that the git-rev-parse manpage describes A...B in the Specifying Ranges section, and everything in that section is only valid in situations where a revision range is valid (i.e. when a revision list is desired).');
ready
Using DOM textNode
var test = safe_tags_dom('if (a < b && c > d) {} // Héllö naõn the context of a revision list, A...B is how git-rev-parse defines it. git-log takes a revision list. git-diff does not take a list of revisions - it takes one or two revisions, and has defined the A...B syntax to mean how its defined in the git-diff manpage. If git-diff did not explicitly define A...B, then that syntax would be invalid. Note that the git-rev-parse manpage describes A...B in the Specifying Ranges section, and everything in that section is only valid in situations where a revision range is valid (i.e. when a revision list is desired).');
ready
Using replace
var test = safe_tags_replace('if (a < b && c > d) {} // Héllö naõn the context of a revision list, A...B is how git-rev-parse defines it. git-log takes a revision list. git-diff does not take a list of revisions - it takes one or two revisions, and has defined the A...B syntax to mean how its defined in the git-diff manpage. If git-diff did not explicitly define A...B, then that syntax would be invalid. Note that the git-rev-parse manpage describes A...B in the Specifying Ranges section, and everything in that section is only valid in situations where a revision range is valid (i.e. when a revision list is desired).');
ready
Using DOM innerText
var test = safe_tags_dom2('if (a < b && c > d) {} // Héllö naõn the context of a revision list, A...B is how git-rev-parse defines it. git-log takes a revision list. git-diff does not take a list of revisions - it takes one or two revisions, and has defined the A...B syntax to mean how its defined in the git-diff manpage. If git-diff did not explicitly define A...B, then that syntax would be invalid. Note that the git-rev-parse manpage describes A...B in the Specifying Ranges section, and everything in that section is only valid in situations where a revision range is valid (i.e. when a revision list is desired).');
 
ready

Revisions

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