split/join vs regex replace (v4)

Revision 4 of this benchmark created on


Preparation HTML

<script>
  var str = '<charlie alpha foxtrot> beta <gamma> delta <omega iota kappa> lambda mu nu xi omicron theta <eta> zeta',
      replacewith = {
      '<': '&#60;',
      '>': '&#62;',
      '"': '&#34;',
      "'": '&#39;',
      '/': '&#x2F;'
      };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
split/join
str.split('<').join('&#60;').split('>').join('&#62;').split("'").join('&#39;').split('"').join('&#34;').split('/').join('&#x2F;');
ready
regex replace
str.replace(/<|>/g, function(a) {
  switch (a) {
  case '<':return '&#60;';
  case '>':return '&#62;';
  case '"':return '&#34;';
  case "'":return '&#39;';
  case "/":return '&#x2F;';
  }
});
ready
regex replace 2
str.replace(/<|>/g, function(a) {
  return replacewith[a];
});
ready

Revisions

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