Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
perf(forEach): use native for loop instead of forEach for Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar committed Jun 30, 2014
1 parent 31ae3e7 commit 36625de
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ function forEach(obj, iterator, context) {
iterator.call(context, obj[key], key);
}
}
} else if (obj.forEach && obj.forEach !== forEach) {
obj.forEach(iterator, context);
} else if (isArrayLike(obj)) {
} else if (isArray(obj) || isArrayLike(obj)) {
for (key = 0, length = obj.length; key < length; key++) {
iterator.call(context, obj[key], key);
}
} else if (obj.forEach && obj.forEach !== forEach) {
obj.forEach(iterator, context);
} else {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
Expand Down

5 comments on commit 36625de

@chinchang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @IgorMinar

This change landed in 1.3.x but not in 1.2.x. Any specific reason?

@IgorMinar
Copy link
Contributor Author

@IgorMinar IgorMinar commented on 36625de Jul 8, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chinchang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Thanks for replying. But this will eventually land in 1.2, right? :)

@Curiosu
Copy link

@Curiosu Curiosu commented on 36625de Aug 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IgorMinar

What do you think about adding a new condition here
...
else if (isArray(obj) || isArrayLike(obj)) {
for (key = 0, length = obj.length; key < length; key++) {
if ( typeof obj[key] !== 'undefined' ) { // ADDED
iterator.call(context, obj[key], key);
}
}
...
to work on sparse arrays, too ?

@caitp
Copy link
Contributor

@caitp caitp commented on 36625de Aug 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just test if the value is undefined in your iterator function if you care about that? (ES5 forEach doesn't care if the property value is undefined or not)

Please sign in to comment.