Slice vs Substr vs Substring vs Replace (v54)

Revision 54 of this benchmark created on


Description

Compare how slicing off top and bottom compares to replacing.

Preparation HTML

<script>
  var longString = "";
  var worstcase = "";
  // Large = 66,000
  for (var i = 0; i < 66000; i++) {
    longString += i % 10;
  }
  for (var i=0; i<66000; i += 14) {
    worstcase += '&lt;script&gt;' ;
  } 

  var re  = /&lt;script&gt;/g;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Substring (long)
longString.substring(10, longString.length-10);
ready
Slice (long)
longString.slice(10, -10);
ready
Substr (long)
longString.substr(10, longString.length -20);
ready
Replace
longString.replace(re,'<script>')
ready
Worst case replace
worstcase.replace(re,'<script>')
ready

Revisions

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