Slice vs Substr vs Substring vs [ ] Methods (v90)

Revision 90 of this benchmark created by jz on


Description

This is comparing the runtimes of [ ], substr, substring, and slice in a string for largish (66000 character) string size.

Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
Substring
  longString.substring(i, i + len);
 
ready
Slice
  longString.slice(i, i + len);
 
ready
Substr
  longString.substr(i, len);
 
ready
[ ] (array)
  resultArray = new Array(len);
  for (j = 0; j < len; j++) {
    resultArray[j] = longString[i + j];
  }
  resultArray.join('');
 
ready
charAt (array)
  resultArray = new Array(len);
  for (j = 0; j < len; j++) {
    resultArray[j] = longString.charAt(i + j);
  }
  resultArray.join('');
 
ready
[ ] (string)
  result = '';
  for (j = 0; j < len; j++) {
    result += longString[i + j];
  }
 
ready
charAt (string)
  result = '';
  for (j = 0; j < len; j++) {
    result += longString.charAt(i + j);
  }
 
ready

Revisions

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