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

Revision 95 of this benchmark created by Dustin Dowell on


Description

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

Preparation HTML

<script>
  var longString = "";
  var word = "";
  // Large = 10,000
  for (var i = 0; i < 10000; i++) {
        longString += i % 10;
  }
  function substr(start, length) {
        substring(start, start+length);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Substring (long)
for (i = 0; i < longString.length; i++) {
  longString.substring(i, i + 10);
}
ready
Slice (long)
for (i = 0; i < longString.length; i++) {
  longString.slice(i, 10);
}
ready
Substr (long)
for (i = 0; i < longString.length; i++) {
  longString.substr(i, 10);
}
ready
[ ] (long)
for (i = 0; i < longString.length; i++) {
 longString[i]+longString[i+1]+ longString[i+2]+longString[i+3] + longString[i+4]+longString[i+5]+ longString[i+6]+longString[i+7]+ longString[i+8]+longString[i+9]+longString[i+10]
}
ready
charAt (long)
for (i = 0; i < longString.length; i++) {
longString.charAt(i)+longString.charAt(i+1)+longString.charAt(i+2)+longString.charAt(i+3)+longString.charAt(i+4)+longString.charAt(i+5)+longString.charAt(i+6)+longString.charAt(i+7)+longString.charAt(i+8)+longString.charAt(i+9)+longString.charAt(i+10);
}
ready

Revisions

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