Slice vs Substr vs Substring vs Replace

JavaScript performance comparison

Revision 54 of this test case created by

Preparation code

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Worst case replace
worstcase.replace(re,'<script>')
pending…
Replace
longString.replace(re,'<script>')
pending…
Substring (long)
longString.substring(10, longString.length-10);
pending…
Slice (long)
longString.slice(10, -10);
pending…
Substr (long)
longString.substr(10, longString.length -20);
pending…

Compare results of other browsers

Revisions

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

0 Comments