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

Commit 55991e3

Browse files
IgorMinarrodyhaddad
authored andcommitted
perf(forEach): cache array length
Micro-optimization :-) BREAKING CHANGE: forEach will iterate only over the initial number of items in the array. So if items are added to the array during the iteration, these won't be iterated over during the initial forEach call. This change also makes our forEach behave more like Array#forEach.
1 parent 8c6a817 commit 55991e3

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/Angular.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ function isArrayLike(obj) {
229229
* @param {Object=} context Object to become context (`this`) for the iterator function.
230230
* @returns {Object|Array} Reference to `obj`.
231231
*/
232+
232233
function forEach(obj, iterator, context) {
233-
var key;
234+
var key, length;
234235
if (obj) {
235236
if (isFunction(obj)) {
236237
for (key in obj) {
@@ -243,8 +244,9 @@ function forEach(obj, iterator, context) {
243244
} else if (obj.forEach && obj.forEach !== forEach) {
244245
obj.forEach(iterator, context);
245246
} else if (isArrayLike(obj)) {
246-
for (key = 0; key < obj.length; key++)
247+
for (key = 0, length = obj.length; key < length; key++) {
247248
iterator.call(context, obj[key], key);
249+
}
248250
} else {
249251
for (key in obj) {
250252
if (obj.hasOwnProperty(key)) {

0 commit comments

Comments
 (0)