Array indexOf

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var array = [];
    for (var i = 0; i < 10000; i++) {
      array[i] = 2 * i;
    }
};
</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
Array loop
var found1 = false, found2 = false;
for (var i = 0; i < array.length; i++) {
  if (array[i] == 100) {
    found1 = i;
    break;
  }
}
for (var i = 0; i < array.length; i++) {
  if (array[i] == 101) {
    found2 = i;
    break;
  }
}
pending…
indexOf
var found1 = array.indexOf(100);
var found2 = array.indexOf(101);
pending…
for in
var found1 = false, found2 = false;
for (var i in array) {
  if (array[i] == 100) {
    found1 = i;
    break;
  }
}
for (var i in array) {
  if (array[i] == 101) {
    found2 = i;
    break;
  }
}
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