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

Commit bdd853c

Browse files
committed
perf(ngRepeat): move work to compile fn
this has impact only on nested repeaters where the number of columns is significant
1 parent e58d65a commit bdd853c

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

src/ng/directive/ngRepeat.js

+38-31
Original file line numberDiff line numberDiff line change
@@ -233,50 +233,56 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
233233
priority: 1000,
234234
terminal: true,
235235
$$tlb: true,
236-
link: function($scope, $element, $attr, ctrl, $transclude){
237-
var expression = $attr.ngRepeat;
238-
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/),
239-
trackByExp, trackByExpGetter, aliasAs, trackByIdExpFn, trackByIdArrayFn, trackByIdObjFn,
240-
lhs, rhs, valueIdentifier, keyIdentifier,
241-
hashFnLocals = {$id: hashKey};
236+
compile: function ngRepeatCompile($element, $attr) {
237+
var expression = $attr.ngRepeat;
238+
var ngRepeatEndComment = document.createComment(' end ngRepeat: ' + expression + ' ');
242239

243-
var ngRepeatEndComment = document.createComment(' end ngRepeat: ' + expression + ' ');
240+
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
244241

245-
if (!match) {
246-
throw ngRepeatMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
242+
if (!match) {
243+
throw ngRepeatMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
247244
expression);
248-
}
245+
}
249246

250-
lhs = match[1];
251-
rhs = match[2];
252-
aliasAs = match[3];
253-
trackByExp = match[4];
247+
var lhs = match[1];
248+
var rhs = match[2];
249+
var aliasAs = match[3];
250+
var trackByExp = match[4];
254251

255-
if (trackByExp) {
256-
trackByExpGetter = $parse(trackByExp);
252+
match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
253+
254+
if (!match) {
255+
throw ngRepeatMinErr('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",
256+
lhs);
257+
}
258+
var valueIdentifier = match[3] || match[1];
259+
var keyIdentifier = match[2];
260+
261+
var trackByExpGetter, trackByIdExpFn, trackByIdArrayFn, trackByIdObjFn;
262+
var hashFnLocals = {$id: hashKey};
263+
264+
if (trackByExp) {
265+
trackByExpGetter = $parse(trackByExp);
266+
} else {
267+
trackByIdArrayFn = function (key, value) {
268+
return hashKey(value);
269+
};
270+
trackByIdObjFn = function (key) {
271+
return key;
272+
};
273+
}
274+
275+
return function ngRepeatLink($scope, $element, $attr, ctrl, $transclude) {
276+
277+
if (trackByExpGetter) {
257278
trackByIdExpFn = function(key, value, index) {
258279
// assign key, value, and $index to the locals so that they can be used in hash functions
259280
if (keyIdentifier) hashFnLocals[keyIdentifier] = key;
260281
hashFnLocals[valueIdentifier] = value;
261282
hashFnLocals.$index = index;
262283
return trackByExpGetter($scope, hashFnLocals);
263284
};
264-
} else {
265-
trackByIdArrayFn = function(key, value) {
266-
return hashKey(value);
267-
};
268-
trackByIdObjFn = function(key) {
269-
return key;
270-
};
271-
}
272-
273-
match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
274-
if (!match) {
275-
throw ngRepeatMinErr('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",
276-
lhs);
277285
}
278-
valueIdentifier = match[3] || match[1];
279-
keyIdentifier = match[2];
280286

281287
// Store a list of elements from previous run. This is a hash where key is the item from the
282288
// iterator, and the value is objects with following properties.
@@ -402,6 +408,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
402408
}
403409
lastBlockMap = nextBlockMap;
404410
});
411+
};
405412
}
406413
};
407414

0 commit comments

Comments
 (0)