JS: For loop vs Array.indexOf

JavaScript performance comparison

Revision 165 of this test case created

Info

Testing speed of a standard for loop vs. String.indexOf vs. Regexp

Preparation code

<script>
  var str = '0, 1, 2, 3, 4, 5, 6, 7, 8, 9';
  var rgx2 = /5/g;
</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
For loop
for (var i = 0; i < str.length; i++) {
 if (str[i] === '5') {
  break;
 }
}
pending…
other for-loop
for (var i = 0, n = str.length; i < n; i++) {
 if (str[i] === '5') {
  break;
 }
}
pending…
indexOf
var a = str.indexOf('5');
pending…
regex
var a = /5/g.exec(str).index;
pending…
while
var i = 0;
while(i !== str.length && str[i] !== '5') i++;
var a = i;
pending…
array
var a = str.split(',').indexOf('5');
pending…
precomp regex
rgx2.lastIndex = 0;
var a = rgx2.exec(str).index;
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