Break Out of Loops as Early as Possible

Benchmark created by BretCameron on


Setup

let found = false;
  const haystack = [...Array(100)].map((e, i) => i + 1);
  const needle = 50;

Test runner

Ready to run.

Testing in
TestOps/sec
Break If Needle Found
for (let i = 0; i < haystack.length; i++) {
  if (haystack[i] === needle) {
    found = true;
    break;
  }
}
ready
Do Not Break
for (let i = 0; i < haystack.length; i++) {
  if (haystack[i] === needle) {
    found = true;
  }
}
ready

Revisions

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

  • Revision 1: published by BretCameron on