regexp vs indexOf - large strings

JavaScript performance comparison

Revision 15 of this test case created by Seamus and last updated

Preparation code

<script>
var str = randomString(1000000);
var re = /a/g;
var i=0;

function randomString(string_length) {
        var chars = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
        var randomstring = '';
        for (var i=0; i<string_length; i++) {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum,rnum+1);
        }
        return randomstring;
}
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Regexp
re.test(str);
pending…
indexOf
i=str.indexOf('a');
if(i>=0) str = str.substr(i)
else str='';
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. Here’s a list of current revisions for this page:

0 comments

Add a comment