Array#indexOf

JavaScript performance comparison

Test case created by Leo Cavalcante

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Native
[1, 2, 3, 4, 5].indexOf(3);
pending…
Alternative (mout)
function indexOf(arr, item, fromIndex) {
  fromIndex = fromIndex || 0;
  var n = arr.length,
      i = fromIndex < 0 ? n + fromIndex : fromIndex;
  while (i < n) {
    // we iterate over sparse items since there is no way to make it
    // work properly on IE 7-8. see #64
    if (arr[i] === item) {
      return i;
    }
    i += 1;
  }
  return -1;
}

indexOf([1, 2, 3, 4, 5], 3);
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