Array IndexOf

JavaScript performance comparison

Test case created by Sergio

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var aArray = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'],
        oArray = {
        'one': true,
        'two': true,
        'three': true,
        'four': true,
        'five': true,
        'six': true,
        'seven': true,
        'eight': true,
        'nine': true,
        'ten': true
        };
   
    function in_array(value, arr) {
      var n = arr.length,
          i;
      for (i = 0; i < n; i++) {
        if (arr[i] === value) {
          return i;
        }
      }
      return -1;
    }
};

Benchmark.prototype.teardown = function() {
    aArray = oArray = null;
};
</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
aArray.indexOf('two');
aArray.indexOf('ten');
aArray.indexOf('fifty');
pending…
in_array
in_array('two', aArray);
in_array('ten', aArray);
in_array('fifty', aArray);
pending…
jQuery inArray
$.inArray('two', aArray);
$.inArray('ten', aArray);
$.inArray('fifty', aArray);
pending…
hash
oArray['two'];
oArray['ten'];
oArray['fifty'];
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