Slice vs Substr vs Substring vs [ ] Methods

JavaScript performance comparison

Revision 90 of this test case created by jz

Preparation code

<script>
  var longString = "";
  // Large = 3,000
  for (var i = 0; i < 3000; i++) {
    longString += i % 10;
  }
var i = 0;
  var len = 250;
</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
Substring
  longString.substring(i, i + len);
pending…
Slice
  longString.slice(i, i + len);
pending…
Substr
  longString.substr(i, len);
pending…
[ ] (array)
  resultArray = new Array(len);
  for (j = 0; j < len; j++) {
    resultArray[j] = longString[i + j];
  }
  resultArray.join('');
pending…
charAt (array)
  resultArray = new Array(len);
  for (j = 0; j < len; j++) {
    resultArray[j] = longString.charAt(i + j);
  }
  resultArray.join('');
pending…
[ ] (string)
  result = '';
  for (j = 0; j < len; j++) {
    result += longString[i + j];
  }
pending…
charAt (string)
  result = '';
  for (j = 0; j < len; j++) {
    result += longString.charAt(i + j);
  }
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