indexOf

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = 'alabama';
    var char = 'a';
   
    String.prototype.allIndexesOf = function(c) {
        var res = [];
        var n = 0;
        while ((n = this.indexOf(c, n)) >= 0) {
            res.push(n++);
        }
        return res;
    }
    function charIndexes(string, char) {
        var i, j, indexes = []; for(i=0,j=string.length;i<j;++i) {
            if(string.charAt(i) === char) {
                indexes.push(i);
            }
        }
   
        return indexes;
    }
};
</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
indexOf
str.allIndexesOf(char);
pending…
own function
charIndexes(str, char);
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