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

Revision 75 of this benchmark created on


Description

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

Preparation HTML

<script>
  var longString = "";
  // Large = 300.000
  for (var i = 0; i < 300000; i++) {
        longString += i % 10;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Substring (long)

  longString.substring(30000);
  longString.substring(250000, 300000);
 
ready
Slice (long)

  longString.slice(30000);
  longString.slice(250000, 300000);
 
ready
Substr (long)

  longString.substr(30000);
  longString.substr(-30000, 0);
 
ready
[ ] (long)
var longString2;
for (i = 30000; i < longString.length; i++) {
  longString2 = +longString[i];

}

for (i = 270000; i < longString.length; i++) {
  longString2 = +longString[i];

}
ready
charAt (long)
var longString2;
for (i = 30000; i < longString.length; i++) {
  longString2 = +longString.charAt(i);

}

for (i = 270000; i < longString.length; i++) {
  longString2 = +longString.charAt(i);

}
ready

Revisions

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