Array Indexing

JavaScript performance comparison

Revision 5 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var parentArray = new Array();
    for (var i = 0; i < 500; i++) {
      parentArray[i] = parentArray[999-i] = i % 30;
    }
};
</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
var found = false;
for (var i = 0; i < parentArray.length; i++) {
  if (parentArray[i] == 29) {
    found = true;
    break;
  }
}
pending…
While
var i = parentArray.length;
while (i--) {
  if (parentArray[i] == 29) {
    found = true;
    break;
  }
}
pending…
indexOf
parentArray.indexOf(29)
pending…
Reversed loop
var found = false;
for (var i = parentArray.length-1; i--;) {
  if (parentArray[i] == 29) {
    found = true;
    break;
  }
}
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