1

I was wondering if anyone knows if Array.find will short circuit the loop when the first item is found. I couldn't find anything googling.

Has anyone done any performance comparisons between find and forEach?

7
  • I think it almost certainly does escape the loop Commented Oct 16, 2019 at 15:53
  • The docs for find explicitly state it returns as soon as the first match is found. Commented Oct 16, 2019 at 15:53
  • 3
    Yes, it does, as the MDN page clearly states Commented Oct 16, 2019 at 15:53
  • 2
    I'm voting to close this question as off-topic because this is not a benchmarking, not programming question Commented Oct 16, 2019 at 15:54
  • ah. not sure how i missed it. it does say immediately return. so it think it's safe to assume it will be more performant than forEach Commented Oct 16, 2019 at 15:56

3 Answers 3

9

The find() method returns the value of the first element in the provided array that satisfies the provided testing function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Array.find will return the first result that matches the callback condition.

Performance wise they're both O(n), albeit find is likely to cycle through less loops, therefore would be more performent in that respect.

Sign up to request clarification or add additional context in comments.

3 Comments

A more relevant quote is the first paragraph of the Description section.
Why is find likely to cycle through fewer loops?
Once it finds the result, it breaks the loop and returns it. forEach will continue cycling through the entire length.
3

I was wondering if anyone knows if Array.find will short circuit the loop when the first item is found.

Yes, it will. This is clear from the spec, which shows it bailing as soon as the callback has returned a truthy value. Step 6(d):

If testResult is true, return kValue.

(Separately, it would be odd to keep searching even though a result had been found. If there had been early spec language doing that, it would have been caught and corrected before being finalized, and/or challenged by the makers of JavaScript engines [and others] as being inefficient.)

Comments

1

As you can see the polyfill code in here, you can notice that find loops until if there is something matched item.

Here is the part of that;

// 6. Repeat, while k < len
      while (k < len) {
        // a. Let Pk be ! ToString(k).
        // b. Let kValue be ? Get(O, Pk).
        // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
        // d. If testResult is true, return kValue.
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return kValue;
        }
        // e. Increase k by 1.
        k++;
      }

So, ∀ n ∈ [0, len - 1]: n <= len - 1

4 Comments

There's no such thing as "the source" of JavaScript. The only source of truth is the spec (and even that doesn't guarantee anything, whether or not it should).
Yes, You are right. It is polyfill code which is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.
@DaveNewton Here is the definition of polyfill. developer.mozilla.org/en-US/docs/Glossary/Polyfill
... I know what a polyfill is. I don't know why its definition is relevant, but thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.