Js StartWith Prototype (v5)

Revision 5 of this benchmark created by Michael Fitzgerald on


Description

string StartWith function to test option in http://stackoverflow.com/questions/646628/javascript-startswith and http://jsperf.com/string-startswith/5

with short string

Preparation HTML

<script>

  function startsWithCharAt(string, pattern) {
    for (var i = 0, length = pattern.length; i < length; i += 1) {
      if (pattern.charAt(i) !== string.charAt(i)) return false;
    }
    return true;
  }
  function startsWithArray(string, pattern) {
    for (var i = 0, length = pattern.length; i < length; i += 1) {
      if (pattern[i] !== string[i]) return false;
    }
    return true;
  }

 var longString1 = 'http://www.google.com';

 var longString2 = 'http';

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
IndexOf
longString1.indexOf(longString2) === 0;
ready
Slice
longString1.slice(0, longString2.length) === longString2;
ready
slice negative
longString1.slice(-longString2.length) === longString2;
ready
substring
longString1.substring(0, longString2.length) === longString2
ready
startsWithCharAt
 startsWithCharAt(longString1, longString2);
ready
startsWithArray
 startsWithArray(longString1, longString2);
ready
lastIndexOf
longString1.lastIndexOf(longString2, 0) === 0
ready

Revisions

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

  • Revision 1: published by Alexandre on
  • Revision 2: published on
  • Revision 3: published by Michael Fitzgerald on
  • Revision 5: published by Michael Fitzgerald on
  • Revision 6: published on
  • Revision 7: published by Doug S on
  • Revision 8: published by Jim Buck on