1

I am developing angular application, in which I used angular.forEach method to iterate over arrays (Not over objects). It is working similar as Array.prototype.forEach. So I am having some questions about using this.

  1. Which method is good for use as per standards.
  2. Which is good as per performance. I tested on jsPerf, It gave same result.
2
  • If I had to guess, I'd say that Angular's forEach will delegate to Array.prototype.forEach if it's available. Commented Aug 31, 2015 at 7:53
  • angular.forEach is pretty much the same as Array.prototype.forEach or jQuery.forEach. It is just wrapping functionality. So it does was a library has to do actually (making the developers life easier). Commented Aug 31, 2015 at 7:55

1 Answer 1

3

According to the angular.forEach-Documentation, it does prevent TypeError(s).

Unlike ES262's Array.prototype.forEach, Providing 'undefined' or 'null' values for obj will not throw a TypeError, but rather just return the value provided.

So it pretty much does was a Library always does. It wraps up some functionality, like checking for null, undefined etc. and handles those occurences, so the developer hasn't to deal with it.

If you want to handle those TypeErrors yourself go with Array.prototype.forEach otherwise use the Library functions.

It won't differ Performance wise.

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

Comments

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.