Slice vs Substr vs Substring Methods

JavaScript performance comparison

Test case created by Kon P and last updated

Info

This is comparing the runtimes of substr, substring, and slice in a string.

Preparation code

<script type="text/javascript">
  var smallString = "",
      mediumString = "",
      longString = "";
  // Small = 100, Medium = 1000, Large = 10,000
  for (var i = 0; i < 100; i++) {
    smallString += i % 10;
    for (var j = 0; j < 10; j++) {
      mediumString += j % 10;
      for (var k = 0; k < 10; k++) {
        longString += k % 10;
      }
    }
  }
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Substring (small)
for (i = 0; i < smallString.length; i++) {
  smallString.substring(i, i + 1);
}
pending…
Slice (small)
for (i = 0; i < smallString.length; i++) {
  smallString.slice(i, i + 1);
}
pending…
Substr (small)
for (i = 0; i < smallString.length; i++) {
  smallString.substr(i, 1);
}
pending…
Substring (medium)
for (i = 0; i < mediumString.length; i++) {
  mediumString.substring(i, i + 1);
}
pending…
Slice (medium)
for (i = 0; i < mediumString.length; i++) {
  mediumString.slice(i, i + 1);
}
pending…
Substr (medium)
for (i = 0; i < mediumString.length; i++) {
  mediumString.substr(i, 1);
}
pending…
Substring (long)
for (i = 0; i < longString.length; i++) {
  longString.substring(i, i + 1);
}
pending…
Slice (long)
for (i = 0; i < longString.length; i++) {
  longString.slice(i, i + 1);
}
pending…
Substr (long)
for (i = 0; i < longString.length; i++) {
  longString.substr(i, 1);
}
pending…

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

Compare results of other browsers

0 comments

Add a comment