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

Revision 8 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;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Substring (long)
for (i = 0; i < longString.length; i++) {
  longString.substring(i, i + 4);
}
ready
Slice (long)
for (i = 0; i < longString.length; i++) {
  longString.slice(i, i + 4);
}
ready
Substr (long)
for (i = 0; i < longString.length; i++) {
  longString.substr(i, 4);
}
ready
[ ] (long)
for (i = 0; i < longString.length; i++) {
  for (j = 0; j < 3; j++) {
    longString[i + j];
  }
}
ready
charAt (long)
for (i = 0; i < longString.length; i++) {
  for (j = 0; j < 3; j++) {
    longString.charAt(i + j);
  }
}
ready

Revisions

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