javascript-collections-framework
    Preparing search index...

    Function binarySearch

    • Returns the first index of list, sorted ascending by comparator (natural order when omitted), whose element compares equal to key, or -1 when none does. The result on an unsorted list is unspecified.

      Time: at most ⌈log₂(n + 1)⌉ + 1 comparator calls and as many get calls.

      Type Parameters

      • E

      Parameters

      • list: ReadonlyList<E>

        A list sorted by comparator.

      • key: E

        The value to find.

      • Optionalcomparator: Comparator<E>

        The order the list is sorted by; natural order when omitted.

      Returns number

      The first matching index, or -1.

      TypeError when list is not a list from this library, comparator is not a function, or it returns a non-number or NaN.

      NullElementError when natural order meets null or undefined.

      ClassCastError when natural order meets values of different kinds.

      ConcurrentModificationError when the comparator changes the list.

      binarySearch(asList([1, 3, 3, 7]), 3) // 1
      binarySearch(asList([1, 3, 3, 7]), 4) // -1