RegExp test / search / match vs. indexOf / substr

JavaScript performance comparison

Revision 65 of this test case created by

Preparation code

<script>
  var str = 'profile-test';
  var re = new RegExp('^profile-', 'i');
  var searchLowercased = 'profile-'.toLowerCase();
  var searchLocaleLowercased = 'profile-'.toLocaleLowerCase();

function customIndexOf(haystack, needle) {
  var tlen = haystack.length;
  var qlen = needle.length;
	var limit = tlen - qlen + 1;

	searchloop: for (var i = 0; i < limit; i++) {
	    for (var j = 0; j < qlen; j++) {
	        if (haystack[j+i] != needle[j]) {
	            continue searchloop;
	        }
	    }
	    return i;
	}
	return -1;
}
</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
indexOfLocale
str.toLocaleLowerCase().indexOf(searchLocaleLowercased) > -1;
pending…
test
re.test(str);
pending…
search
str.search(re) > -1;
pending…
match
str.match(re).length > 0;
pending…
indexOf
str.toLowerCase().indexOf(searchLowercased) > -1;
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