Find item in an array

JavaScript performance comparison

Test case created by nikitahl

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var numbers = [];
  
  var number = 1000000
  var number1 = 0;
  var number2 = number/2;
  var number3 = number - 1;
  
  for (var i = 0; i < number; i++) {
    numbers.push(i);
  }
  
  function forLoop(value, arr) {
    var result;
    for (var i = 0; i < arr.length; i++) {
      if (value === arr[i]) {
        result = i;
        break;
      }
    }
    return result;
  }
  

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
indexOf()
numbers.indexOf(number1);
numbers.indexOf(number2);
numbers.indexOf(number3);
pending…
findIndex()
numbers.findIndex(number => number === number1);
numbers.findIndex(number => number === number2);
numbers.findIndex(number => number === number3);
pending…
find()
numbers.find(number => number === number1);
numbers.find(number => number === number2);
numbers.find(number => number === number3);
pending…
for loop
forLoop(number1, numbers);
forLoop(number2, numbers);
forLoop(number3, numbers);
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.

0 Comments