Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/113.0
Firefox 113
Mac OS X 10.15
Desktop
one year ago
Test name Executions per second
find 3133.5 Ops/sec
For-loop 3858.3 Ops/sec
while 3186.9 Ops/sec
Tests:
  • find

    x
     
    const arr = Array.from({ length: 20000 }, (_, i) => ({ property: i }))
    const elementToFind = 15000;
    const element = arr.find((el) => el.property === elementToFind);
  • For-loop

     
    const arr = Array.from({ length: 20000 }, (_, i) => ({ property: i }))
    const elementToFind = 15000;
    let element = undefined;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].property === elementToFind) {
          element = arr[i];
          break;
        }
    }
  • while

     
    const arr = Array.from({ length: 20000 }, (_, i) => ({ property: i }))
    const elementToFind = 15000;
    let element = undefined;
    let i = 0;
    while(element === undefined) {
      if(arr[i].property === elementToFind) {
        element = elementToFind;
      }
      i++;
    }