JS startsWith

JavaScript performance comparison

Revision 4 of this test case created

Info

Perf-testing a JS startsWith() function

Preparation code

<script>
  // Methods to test
  // String.prototype.startsWith1 using ===
  String.prototype.startsWith1 = function(str) {
   return (this.indexOf(str) === 0);
  };
 
  // String.prototype.startsWith using !
  String.prototype.startsWith2 = function(str) {
   return !this.indexOf(str);
  }
 
  // Using substr
  String.prototype.subStringChk = function(str) {
   return this.substr(0, str.length) === str;
  };
 
 
 
  // Using lastIndexOf === 0
  String.prototype.lastIndexChk = function(str) {
   return this.lastIndexOf(str, 0) === 0;
  };
 
 
  // sample data
  var origString = 'hello, world';
  var testString = 'he';
</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
String.prototype.startsWith1
origString.startsWith1(testString);
pending…
String.prototype.startsWith2
origString.startsWith2(testString);
pending…
subStringChk
origString.subStringChk(testString);
pending…
lastIndexChk
origString.lastIndexChk(testString);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment