Skip to content

Commit 37d627d

Browse files
perf(core): minimze trackBy calculations (#52227)
Re-organize code to minimize number of calls to the trackBy function. PR Close #52227
1 parent 1032c1e commit 37d627d

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

packages/core/src/render3/list_reconciliation.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,23 @@ export function reconcile<T, V>(
124124
continue;
125125
}
126126

127-
// Detect swap / moves:
127+
// Detect swap and moves:
128128
const liveStartKey = trackByFn(liveStartIdx, liveStartValue);
129129
const liveEndKey = trackByFn(liveEndIdx, liveEndValue);
130130
const newStartKey = trackByFn(liveStartIdx, newStartValue);
131-
const newEndKey = trackByFn(newEndIdx, newEndValue);
132-
if (Object.is(newStartKey, liveEndKey) && Object.is(newEndKey, liveStartKey)) {
133-
// swap on both ends;
134-
liveCollection.swap(liveStartIdx, liveEndIdx);
135-
liveCollection.updateValue(liveStartIdx, newStartValue);
136-
liveCollection.updateValue(liveEndIdx, newEndValue);
137-
newEndIdx--;
138-
liveStartIdx++;
139-
liveEndIdx--;
140-
continue;
141-
} else if (Object.is(newStartKey, liveEndKey)) {
142-
// the new item is the same as the live item with the end pointer - this is a move forward
143-
// to an earlier index;
144-
liveCollection.move(liveEndIdx, liveStartIdx);
131+
if (Object.is(newStartKey, liveEndKey)) {
132+
const newEndKey = trackByFn(newEndIdx, newEndValue);
133+
// detect swap on both ends;
134+
if (Object.is(newEndKey, liveStartKey)) {
135+
liveCollection.swap(liveStartIdx, liveEndIdx);
136+
liveCollection.updateValue(liveEndIdx, newEndValue);
137+
newEndIdx--;
138+
liveEndIdx--;
139+
} else {
140+
// the new item is the same as the live item with the end pointer - this is a move forward
141+
// to an earlier index;
142+
liveCollection.move(liveEndIdx, liveStartIdx);
143+
}
145144
liveCollection.updateValue(liveStartIdx, newStartValue);
146145
liveStartIdx++;
147146
continue;
@@ -188,8 +187,6 @@ export function reconcile<T, V>(
188187
while (!newIterationResult.done && liveStartIdx <= liveEndIdx) {
189188
const liveValue = liveCollection.at(liveStartIdx);
190189
const newValue = newIterationResult.value;
191-
const newKey = trackByFn(liveStartIdx, newValue);
192-
const liveKey = trackByFn(liveStartIdx, liveValue);
193190
const isStartMatching =
194191
valuesMatching(liveStartIdx, liveValue, liveStartIdx, newValue, trackByFn);
195192
if (isStartMatching !== 0) {
@@ -205,6 +202,7 @@ export function reconcile<T, V>(
205202
initLiveItemsInTheFuture(liveCollection, liveStartIdx, liveEndIdx, trackByFn);
206203

207204
// Check if I'm inserting a previously detached item: if so, attach it here
205+
const newKey = trackByFn(liveStartIdx, newValue);
208206
if (attachPreviouslyDetached(liveCollection, detachedItems, liveStartIdx, newKey)) {
209207
liveCollection.updateValue(liveStartIdx, newValue);
210208
liveStartIdx++;
@@ -217,6 +215,7 @@ export function reconcile<T, V>(
217215
newIterationResult = newCollectionIterator.next();
218216
} else {
219217
// it is a move forward - detach the current item without advancing in collections
218+
const liveKey = trackByFn(liveStartIdx, liveValue);
220219
detachedItems.set(liveKey, liveCollection.detach(liveStartIdx));
221220
liveEndIdx--;
222221
}

0 commit comments

Comments
 (0)