Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Chrome 109
Windows
Desktop
one year ago
Test name Executions per second
find 592.3 Ops/sec
For-loop 639.1 Ops/sec
while 255.8 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++;
    }