You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2022/11/18 17:51:20 UTC

[GitHub] [trafficcontrol] ocket8888 commented on a diff in pull request #7187: Minimize Snapshot diff for Server Server Capabilities

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: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org