startsWith

JavaScript performance comparison

Revision 47 of this test case created by

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  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.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;
  }

};
</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
indexOf
r1 = (s.indexOf("test") == 0);
r2 = (s.indexOf("not there") == 0);
pending…
lastIndexOf
r1 = (s.lastIndexOf("test", 0) == 0);
r2 = (s.lastIndexOf("not there", 0) == 0);
pending…
substring
r1 = (s.substring(0, "test".length) == "test");
r2 = (s.substring(0, "not there".length) == "not there");
pending…
slice
r1 = (s.slice(0, "test".length) == "test");
r2 = (s.slice(0, "not there".length) == "not there");
pending…
regex
r1 = (/^test/i).test(s);
r2 = (/^not there/i).test(s);
pending…
startsWithCharAt
r1 = s.startsWithCharAt("test");
r2 = s.startsWithCharAt("not there");
pending…
startsWithCharCodeAt
r1 = s.startsWithCharCodeAt("test");
r2 = s.startsWithCharCodeAt("not there");
pending…
startsWithSubstring
r1 = s.startsWithSubstring("test");
r2 = s.startsWithSubstring("not there");
pending…
startsWith (Browser ES6)
r1 = s.startsWith("test");
r2 = s.startsWith("not there");
pending…
startsWithSlice
r1 = s.startsWithSlice("test");
r2 = s.startsWithSlice("not there");
pending…
startsWithSlice2
r1 = s.startsWithSlice2("test");
r2 = s.startsWithSlice2("not there");
pending…
Lodash startsWith
r1 = _.startsWith(s, 'test');
r2 = _.startsWith(s, 'not there');
pending…
Lodash startsWith 0
r1 = _.startsWith(s, 'test', 0);
r2 = _.startsWith(s, 'not there', 0);
pending…
lodashlike
r1 = lodashlike(s, 'test', 0);
r2 = lodashlike(s, 'not there', 0);
pending…
lodashlikenostringvalidation
r1 = lodashlikenostringvalidation(s, 'test', 0);
r2 = lodashlikenostringvalidation(s, 'not there', 0);
pending…
lodashlike2
r1 = lodashlike2(s, 'test', 0);
r2 = lodashlike2(s, 'not there', 0);
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