JS startsWith (v4)

Revision 4 of this benchmark created on


Description

Perf-testing a JS startsWith() function

Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
String.prototype.startsWith1
origString.startsWith1(testString);
ready
String.prototype.startsWith2
origString.startsWith2(testString);
ready
subStringChk
origString.subStringChk(testString);
ready
lastIndexChk
origString.lastIndexChk(testString);
ready

Revisions

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