indexof vs regex vs regex i

JavaScript performance comparison

Test case created by juloo

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$';
    var str_i = 'abcdefghijklmnopqrstuvwxyz0123456789_$';
   
    var reg = /^[a-zA-Z0-9\$_]{1}$/;
    var reg_i = /^[a-z0-9\$_]{1}$/i;
   
    var tests = ['r', '8', 'p', '*', 't', '.', '{', 'z', '9', '2']
   
    var test = false;
};
</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
for (var i = 0; i < tests.length; i++) {
  test = (str.indexOf(tests[i]) >= 0);
}
pending…
Indexof no case
for (var i = 0; i < tests.length; i++) {
  test = (str_i.indexOf(tests[i].toLowerCase()) >= 0);
}
pending…
Regex
for (var i = 0; i < tests.length; i++) {
  test = reg.test(tests[i]);
}
pending…
Regex no case
for (var i = 0; i < tests.length; i++) {
  test = reg_i.test(tests[i]);
}
pending…

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

Compare results of other browsers

0 comments

Add a comment