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

Revision 81 of this benchmark created on


Description

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

Preparation HTML

<script>
  var longString = "";
  // Large = 66,000
  for (var i = 0; i < 50; i++) {
    longString += i % 10;
  }
var i = 5;
  var len = 10;
</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.