startsWith (v55)

Revision 55 of this benchmark created by Alvaro on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.js"></script>

Setup

a = ["test"];
  for (var i = 0; i < 10000; i++) {
    a.push("some Other Stuff");
  }
  s = a.join();
  
  String.prototype.startsWithSubstring = function (pattern) {
      return pattern != null && this.length >= pattern.length && this.substring(0, pattern.length) === pattern;
  }
  
  String.prototype.startsWithSlice = function (pattern) {
      return pattern != null && this.length >= pattern.length && this.slice(0, pattern.length) === pattern;
  }
  
  String.prototype.startsWithSlice2 = function (pattern) {
      return pattern !== undefined && pattern !== null && this.length >= pattern.length && this.slice(0, pattern.length) === pattern;
  }
  
  String.prototype.startsWithIndexOf = function (searchString, position) {
      position = position || 0;
      return this.indexOf(searchString, position) === position;
  }
  
  String.prototype.startsWithCharAt = function (pattern) {
        if (pattern === undefined || pattern === null) return false;
        var length = pattern.length; 
        for (var i = 0; i < length; i++) {
          if (pattern.charAt(i) !== this.charAt(i)) return false;
        }
        return true;
  }
  
  String.prototype.startsWithCharCodeAt = function (pattern) {
        if (pattern === undefined || pattern === null) return false;
        var length = pattern.length; 
        for (var i = 0; i < length; i++) {
          if (pattern.charCodeAt(i) !== this.charCodeAt(i)) return false;
        }
        return true;
  }
  
  function lodashlike(string, target, position) {
        string = string == null ? '' : (string + '' );
        position = position == null
          ? 0
          : Math.min(position < 0 ? 0 : (+position || 0), string.length);
  
        return string.lastIndexOf(target, position) == position;
  }
  
  function lodashlike2(string, target, position) {
        if (string == null) {
          string = '';
        } else if (typeof string != 'string') {
          string = string + '';
        }
        position = position == null
          ? 0
          : Math.min(position < 0 ? 0 : (+position || 0), string.length);
  
        return string.lastIndexOf(target, position) == position;
  }
  
  function lodashlikenostringvalidation(string, target, position) {
        position = position == null
          ? 0
          : Math.min(position < 0 ? 0 : (+position || 0), string.length);
  
        return string.lastIndexOf(target, position) == position;
  }
  
  // Simple and predictable means fast and optimizable.
  function startsWithLoop(haystack, needle, position) {
        position = position == null
          ? 0
          : Math.min(position < 0 ? 0 : (+position || 0), string.length);
  
      var ceil = needle.length;
      for(var i = position; i < ceil; i++) {
              if(haystack[i] != needle[i]) return false;
      }
      return true;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
startsWith Loop
r1 = startsWithLoop(s, 'test');
r2 = startsWithLoop(s, 'not there');
ready
lastIndexOf
r1 = (s.lastIndexOf("test", 0) == 0);
r2 = (s.lastIndexOf("not there", 0) == 0);
ready
substring
r1 = (s.substring(0, "test".length) == "test");
r2 = (s.substring(0, "not there".length) == "not there");
ready
slice
r1 = (s.slice(0, "test".length) == "test");
r2 = (s.slice(0, "not there".length) == "not there");
ready
startsWithIndexOf
r1 = s.startsWithIndexOf("test");
r2 = s.startsWithIndexOf("not there");
ready
startsWithCharAt
r1 = s.startsWithCharAt("test");
r2 = s.startsWithCharAt("not there");
ready
startsWithCharCodeAt
r1 = s.startsWithCharCodeAt("test");
r2 = s.startsWithCharCodeAt("not there");
ready
startsWithSubstring
r1 = s.startsWithSubstring("test");
r2 = s.startsWithSubstring("not there");
ready
startsWith (Browser ES6)
r1 = s.startsWith("test");
r2 = s.startsWith("not there");
ready
startsWithSlice
r1 = s.startsWithSlice("test");
r2 = s.startsWithSlice("not there");
ready
startsWithSlice2
r1 = s.startsWithSlice2("test");
r2 = s.startsWithSlice2("not there");
ready
Lodash startsWith
r1 = _.startsWith(s, 'test');
r2 = _.startsWith(s, 'not there');
ready
Lodash startsWith 0
r1 = _.startsWith(s, 'test', 0);
r2 = _.startsWith(s, 'not there', 0);
ready
lodashlike
r1 = lodashlike(s, 'test', 0);
r2 = lodashlike(s, 'not there', 0);
ready
lodashlikenostringvalidation
r1 = lodashlikenostringvalidation(s, 'test', 0);
r2 = lodashlikenostringvalidation(s, 'not there', 0);
ready
lodashlike2
r1 = lodashlike2(s, 'test', 0);
r2 = lodashlike2(s, 'not there', 0);
ready
indexOf
r1 = (s.indexOf("test") == 0);
r2 = (s.indexOf("not there") == 0);
ready

Revisions

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