From 9e12886c6648688e746df48fd4ca923c68e2973a Mon Sep 17 00:00:00 2001 From: Pete Richards Date: Fri, 21 Apr 2017 11:38:24 -0700 Subject: [PATCH] Shortcut index check for append/prepend Update the insertion point check with shortcutting behavior for appending / prepending objects, which is the common case for sorted inserts on initial table load (when large numbers of records are inserted). This allows O(1) performance for the common case while maintaining O(log n) performance for the edge case. --- .../src/controllers/MCTTableController.js | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/platform/features/table/src/controllers/MCTTableController.js b/platform/features/table/src/controllers/MCTTableController.js index 45c3be85ee..52d1c2f7f4 100644 --- a/platform/features/table/src/controllers/MCTTableController.js +++ b/platform/features/table/src/controllers/MCTTableController.js @@ -436,9 +436,25 @@ define( * @param {Object} searchElement Object to find the insertion point for */ MCTTableController.prototype.findInsertionPoint = function (searchArray, searchElement) { - //First, use a binary search to find the correct insertion point - var index = this.binarySearch(searchArray, searchElement, 0, searchArray.length - 1); - var testIndex = index; + var index; + var textIndex; + var first = searchArray[0]; + var last = searchArray[searchArray.length - 1]; + + // Shortcut check for append/prepend + if (first && this.sortComparator(first, searchElement) <= 0) { + index = testIndex = 0; + } else if (last && this.sortComparator(last, searchElement) >= 0) { + index = testIndex = searchArray.length - 1; + } else { + // use a binary search to find the correct insertion point + index = testIndex = this.binarySearch( + searchArray, + searchElement, + 0, + searchArray.length - 1 + ); + } //It's possible that the insertion point is a duplicate of the element to be inserted var isDupe = function () {