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

Revision 80 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 < 66000; i++) {
    longString += i % 10;
  }
  var len = 1;
</script>

Test runner

Ready to run.

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