ocket8888 commented on code in PR #7187:
URL: https://github.com/apache/trafficcontrol/pull/7187#discussion_r1026721927


##########
traffic_portal/app/src/common/service/utils/CollectionUtils.js:
##########
@@ -19,6 +19,39 @@
 
 var CollectionUtils = function() {
 
+       // minimizeArrayDiff reduces the size of an index-sensitive Array diff 
by
+       // making common elements in the old and new arrays have the same index.
+       this.minimizeArrayDiff = function (oldItems, newItems) {
+               const minimalDiffItems = [];
+               const addedItems = [];
+
+               let newItemsIndex, newItem;
+               const oldItemsIterator = oldItems.entries();
+               const newItemsIterator = newItems.entries();
+               for (let oldItemsNext = oldItemsIterator.next(), newItemsNext = 
newItemsIterator.next(); !(oldItemsNext.done || newItemsNext.done);) {
+                       const [, oldItem] = oldItemsNext.value;
+                       [newItemsIndex, newItem] = newItemsNext.value;
+                       if (oldItem < newItem) {
+                               newItemsIndex--;
+                               minimalDiffItems.push(undefined);
+                               oldItemsNext = oldItemsIterator.next();
+                               continue;
+                       } else if (oldItem > newItem) {
+                               addedItems.push(newItem);
+                               newItemsNext = newItemsIterator.next();
+                               continue;
+                       }
+                       minimalDiffItems.push(newItem);
+                       oldItemsNext = oldItemsIterator.next();
+                       newItemsNext = newItemsIterator.next();
+               }
+               minimalDiffItems.push(...addedItems);

Review Comment:
   fyi, there's a builtin for just concatenating two arrays without doing a 
spread operation into a push: 
[`Array.prototype.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to