Slice vs Substr vs Substring vs [ ] Methods

JavaScript performance comparison

Revision 94 of this test case created by

Preparation code

<script>
  var longString = "";
  var word = "";
  // Large = 10,000
  for (var i = 0; i < 10000; i++) {
        longString += i % 10;
  }
  function substr(start, length) {
        substring(start, start+length);
  }
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Substring (long)
for (i = 0; i < longString.length; i++) {
  longString.substring(i, i + 10);
}
pending…
Slice (long)
for (i = 0; i < longString.length; i++) {
  longString.slice(i, i + 10);
}
pending…
Substr (long)
for (i = 0; i < longString.length; i++) {
  longString.substr(i, 10);
}
pending…
[ ] (long)
for (i = 0; i < longString.length; i++) {
 longString[i]+longString[i+1]+ longString[i+2]+longString[i+3] + longString[i+4]+longString[i+5]+ longString[i+6]+longString[i+7]+ longString[i+8]+longString[i+9]+longString[i+10]
}
pending…
charAt (long)
for (i = 0; i < longString.length; i++) {
longString.charAt(i)+longString.charAt(i+1)+longString.charAt(i+2)+longString.charAt(i+3)+longString.charAt(i+4)+longString.charAt(i+5)+longString.charAt(i+6)+longString.charAt(i+7)+longString.charAt(i+8)+longString.charAt(i+9)+longString.charAt(i+10);
}
pending…

Compare results of other browsers

Revisions

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

0 Comments