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

Commit e58d65a

Browse files
committed
perf(ngRepeat): move updateScope fn to factory and reuse it for all repeaters
1 parent fbd4884 commit e58d65a

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/ng/directive/ngRepeat.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@
211211
var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
212212
var NG_REMOVED = '$$NG_REMOVED';
213213
var ngRepeatMinErr = minErr('ngRepeat');
214+
215+
var updateScope = function(scope, index, valueIdentifier, value, keyIdentifier, key, arrayLength) {
216+
// TODO(perf): generate setters to shave off ~40ms or 1-1.5%
217+
scope[valueIdentifier] = value;
218+
if (keyIdentifier) scope[keyIdentifier] = key;
219+
scope.$index = index;
220+
scope.$first = (index === 0);
221+
scope.$last = (index === (arrayLength - 1));
222+
scope.$middle = !(scope.$first || scope.$last);
223+
// jshint bitwise: false
224+
scope.$odd = !(scope.$even = (index&1) === 0);
225+
// jshint bitwise: true
226+
};
227+
228+
214229
return {
215230
restrict: 'A',
216231
multiElement: true,
@@ -291,18 +306,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
291306
$scope[aliasAs] = collection;
292307
}
293308

294-
var updateScope = function(scope, index) {
295-
scope[valueIdentifier] = value;
296-
if (keyIdentifier) scope[keyIdentifier] = key;
297-
scope.$index = index;
298-
scope.$first = (index === 0);
299-
scope.$last = (index === (arrayLength - 1));
300-
scope.$middle = !(scope.$first || scope.$last);
301-
// jshint bitwise: false
302-
scope.$odd = !(scope.$even = (index&1) === 0);
303-
// jshint bitwise: true
304-
};
305-
306309
if (isArrayLike(collection)) {
307310
collectionKeys = collection;
308311
trackByIdFn = trackByIdExpFn || trackByIdArrayFn;
@@ -379,7 +382,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
379382
$animate.move(getBlockNodes(block.clone), null, jqLite(previousNode));
380383
}
381384
previousNode = getBlockEnd(block);
382-
updateScope(block.scope, index);
385+
updateScope(block.scope, index, valueIdentifier, value, keyIdentifier, key, arrayLength);
383386
} else {
384387
// new item which we don't know about
385388
$transclude(function ngRepeatTransclude(clone, scope) {
@@ -393,7 +396,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
393396
// by a directive with templateUrl when its template arrives.
394397
block.clone = clone;
395398
nextBlockMap[block.id] = block;
396-
updateScope(block.scope, index);
399+
updateScope(block.scope, index, valueIdentifier, value, keyIdentifier, key, arrayLength);
397400
});
398401
}
399402
}

0 commit comments

Comments
 (0)