startsWith speed comparison

JavaScript performance comparison

Revision 59 of this test case created by

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.string/3.0.0/underscore.string.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  a = ["test"];
  for (var i = 0; i < 10000; i++) {
    a.push("some other stuff");
  }
  str = a.join();
  
  String.prototype.startsWith = function(needle) {
  	return this.slice(0, needle.length) === needle;
  };
  
  String.prototype.startsWithLoop = function(needle) {
    var that = this;
  	var ceil = needle.length;
  	for(var i = 0; i < ceil; i++) {
  		if(that[i] !== needle[i]) return false;
  	}
  	return true;
  }
  
  function startsWith(haystack, needle) {
  	return haystack.slice(0, needle.length) === needle;
  }
  
  function startsWithLoop(haystack, needle) {
  	var ceil = needle.length;
  	for(var i = 0; i < ceil; i++) {
  		if(haystack[i] !== needle[i]) return false;
  	}
  	return true;
  }
  
  function startsWithLoopCharAt(haystack, needle) {
  	var ceil = needle.length;
  	for(var i = 0; i < ceil; i++) {
  		if(haystack.charAt(i) !== needle.charAt(i)) return false;
  	}
  	return true;
  }

};
</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

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