```typescript
/** The index of target in a sorted array, or -1. */
export function binarySearch(items: readonly number[], target: number): number {
  if (items.length === 0) return -1; // Early check for empty array

  let low = 0;
  let high = items.length - 1;

  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    if (items[mid] === target) {
      return mid;
    }
    if (items[mid] < target) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }

  // If the loop exits without finding the target, it means it has been found at the upper bound
  return items[high] === target ? high : -1;
}
```