reduce vs. findIndex (v2)

Revision 2 of this benchmark created on


Setup

let arr = [0, 2, 4, 5, 7, 9, 11];

Test runner

Ready to run.

Testing in
TestOps/sec
reduce
let int = Math.floor(Math.random() * 12);
arr.reduce((prev, curr, idx) => curr <= int ? idx : prev);
ready
findIndex
let int = Math.floor(Math.random() * 12);
arr.findIndex((elt, idx, a) => {
  return (
    elt === int ||
    idx === 6 ||
    (elt < int && a[idx + 1] > int)
  );
});
ready
for loop
let int = Math.floor(Math.random() * 12);
let arr = [0, 2, 4, 5, 7, 9, 11];
let idx;
for (idx = 0; idx < 7; idx++) {
  let elt = arr[idx];
  if (elt === int || idx === 6 || (elt < int && arr[idx + 1] > int)) {
    break;
  }
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.