mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-08 22:37:20 +08:00
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.
This commit is contained in:
@@ -229,8 +229,9 @@ function isArrayLike(obj) {
|
||||
* @param {Object=} context Object to become context (`this`) for the iterator function.
|
||||
* @returns {Object|Array} Reference to `obj`.
|
||||
*/
|
||||
|
||||
function forEach(obj, iterator, context) {
|
||||
var key;
|
||||
var key, length;
|
||||
if (obj) {
|
||||
if (isFunction(obj)) {
|
||||
for (key in obj) {
|
||||
@@ -243,8 +244,9 @@ function forEach(obj, iterator, context) {
|
||||
} else if (obj.forEach && obj.forEach !== forEach) {
|
||||
obj.forEach(iterator, context);
|
||||
} else if (isArrayLike(obj)) {
|
||||
for (key = 0; key < obj.length; key++)
|
||||
for (key = 0, length = obj.length; key < length; key++) {
|
||||
iterator.call(context, obj[key], key);
|
||||
}
|
||||
} else {
|
||||
for (key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
|
||||
Reference in New Issue
Block a user