es2015/object-store/query-engine.js
function findInsertIndex(records, record, comparator) {
  if (!records.length) {
    return 0
  }
  
  if (records.length === 1) {
    let comparison = comparator(records[0].record, record)
    return (comparison > 0) ? 0 : 1
  }
  
  let comparison = comparator(records[0].record, record)
  if (comparison > 0) {
    return 0
  }
  
  let bottom = 1
  let top = records.length - 1
  
  while (bottom <= top) {
    let pivotIndex = Math.floor((bottom + top) / 2)
    
    let comparison = comparator(records[pivotIndex].record, record)
    if (comparison > 0) {
      let previousElement = records[pivotIndex - 1].record
      if (comparator(previousElement, record) <= 0) {
        return pivotIndex
      }
      
      top = pivotIndex - 1
    } else {
      bottom = pivotIndex + 1
    }
  }
  
  return records.length
}
function findInsertIndex(records, record, comparator) {
  if (!records.length) {
    return 0
  }
  
  if (records.length === 1) {
    let comparison = comparator(records[0].record, record)
    return (comparison > 0) ? 0 : 1
  }
  
  let comparison = comparator(records[0].record, record)
  if (comparison > 0) {
    return 0
  }
  
  let bottom = 1
  let top = records.length - 1
  
  while (bottom <= top) {
    let pivotIndex = Math.floor((bottom + top) / 2)
    
    let comparison = comparator(records[pivotIndex].record, record)
    if (comparison > 0) {
      let previousElement = records[pivotIndex - 1].record
      if (comparator(previousElement, record) <= 0) {
        return pivotIndex
      }
      
      top = pivotIndex - 1
    } else {
      bottom = pivotIndex + 1
    }
  }
  
  return records.length
}
Uses the binary search algorithm to find the index at which the specified
record should be inserted into the specified array of records to keep the
array sorted according to the provided comparator.

@param {{record: *, primaryKey: (number|string|Date|Array)}[]} records The
array of records into which the provided record should be inserted.
@param {*} record The record to insert into the records array.
@param {function(*, *): number} comparator Record comparator by which the
array is sorted. The comparator is a standard comparator function
compatible with the {@linkcode Array.prototype.sort} method.
@return {number} The index at which the record should be inserted to keep
the array of records sorted.