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.
This commit is contained in:
Pete Richards 2017-04-21 11:38:24 -07:00 committed by Victor Woeltjen
parent 2d352ac574
commit 9e12886c66

View File

@ -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 () {