Slice vs Substr vs Substring Methods (v125)

Revision 125 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 = "";
  // Large = 10,000
  for (var i = 0; i < 10000; i++) {
        longString += i % 10;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Substring (long)
for (var i = 0; i < longString.length; i++) {
var s = Math.random() * longString.length;
  var r = longString.substring(0, r) + longString.substring(r+1, longString.length);
}
ready
Slice (long)
for (var i = 0; i < longString.length; i++) {
var s = Math.random() * longString.length;
  var r = longString.slice(0, r) + longString.slice(r+1, longString.length);
}
ready
Substr (long)
for (var i = 0; i < longString.length; i++) {
var s = Math.random() * longString.length;
  var r = longString.substr(0, r) + longString.substr(r+1, longString.length);
}
ready

Revisions

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