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

Revision 28 of this benchmark created 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 = 1,000,000
  for (var i = 0; i < 1000000; i++) {
        longString += i % 10;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Substring (long)
for (var i = 0, n = longString.length; i < n; i++) {
  longString.substring(i, 40000);
}
ready
Slice (long)
for (var i = 0, n = longString.length; i < n; i++) {
  longString.slice(i, i + 40000);
}
ready
Substr (long)
for (var i = 0, n = longString.length; i < n; i++) {
  longString.substr(i, 40000);
}
ready

Revisions

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