Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Chrome 131
Windows
Desktop
2 months ago
Test name Executions per second
reversed for loop 4873665.5 Ops/sec
find method 12336973.0 Ops/sec
Script Preparation code:
AخA
 
var array = [];
for (let i = 0 ; i < 10000 ; i++) {
    array.push (Math.floor (Math.random () * 100));
}
Tests:
  • reversed for loop

     
    const value = Math.floor (Math.random () * 100);
    let found = undefined;
    for (let i =  array.length - 1 ; i >= 0 ; i--) {
        if (array[i] === value) {
            found = value;
            break;
        }
    }
  • find method

     
    const value = Math.floor (Math.random () * 100);
    let found = array.find ( (item) => item === value);