RegExp test / search / match vs. indexOf / substr (v65)

Revision 65 of this benchmark created on


Description

Testing for the existence of characters in a string using Regular Expressions test, search, match and substr compared to indexOf.

Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
test
re.test(str);
ready
search
str.search(re) > -1;
ready
match
str.match(re).length > 0;
ready
indexOf
str.toLowerCase().indexOf(searchLowercased) > -1;
ready
indexOfLocale
str.toLocaleLowerCase().indexOf(searchLocaleLowercased) > -1;
ready

Revisions

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