perf(ngRepeat): simplify code and remove duplicate array.length access

minimal perf gain (~2ms)
This commit is contained in:
Igor Minar
2014-08-12 22:12:53 -07:00
parent 2ef25887ef
commit 08eb05583b

View File

@@ -310,13 +310,13 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
// Same as lastBlockMap but it has the current state. It will become the
// lastBlockMap on the next iteration.
nextBlockMap = createMap(),
arrayLength,
collectionLength,
key, value, // key/value of iteration
trackById,
trackByIdFn,
collectionKeys,
block, // last object information {scope, element, id}
nextBlockOrder = [],
nextBlockOrder,
elementsToRemove;
if (aliasAs) {
@@ -338,11 +338,11 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
collectionKeys.sort();
}
arrayLength = collectionKeys.length;
collectionLength = collectionKeys.length;
nextBlockOrder = new Array(collectionLength);
// locate existing items
length = nextBlockOrder.length = collectionKeys.length;
for (index = 0; index < length; index++) {
for (index = 0; index < collectionLength; index++) {
key = (collection === collectionKeys) ? index : collectionKeys[index];
value = collection[key];
trackById = trackByIdFn(key, value, index);
@@ -382,7 +382,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
}
// we are not using forEach for perf reasons (trying to avoid #call)
for (index = 0, length = collectionKeys.length; index < length; index++) {
for (index = 0; index < collectionLength; index++) {
key = (collection === collectionKeys) ? index : collectionKeys[index];
value = collection[key];
block = nextBlockOrder[index];
@@ -401,7 +401,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
$animate.move(getBlockNodes(block.clone), null, jqLite(previousNode));
}
previousNode = getBlockEnd(block);
updateScope(block.scope, index, valueIdentifier, value, keyIdentifier, key, arrayLength);
updateScope(block.scope, index, valueIdentifier, value, keyIdentifier, key, collectionLength);
} else {
// new item which we don't know about
$transclude(function ngRepeatTransclude(clone, scope) {
@@ -415,7 +415,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
// by a directive with templateUrl when its template arrives.
block.clone = clone;
nextBlockMap[block.id] = block;
updateScope(block.scope, index, valueIdentifier, value, keyIdentifier, key, arrayLength);
updateScope(block.scope, index, valueIdentifier, value, keyIdentifier, key, collectionLength);
});
}
}