param source param value param compareFn param startIndex param endIndex export function binarySearch < T > ( source , value , compareFn , startIndex = 0 , endIndex = source . length - 1 ) { if ( startIndex < 0 ) { throw new TypeError ( "startIndex is out of range" ) ; } if ( endIndex >= source . length ) { throw new TypeError ( "endIndex is out of range" ) ; } const length = endIndex + 1 - startIndex ; let low = startIndex ; let high = ( startIndex + length ) - 1 ; while ( low <= high ) { const i = low + ( ( high - low ) >>> 1 ) ; const cp = compareFn ( source [ i ] , value ) ; if ( cp === 0 ) { return i ; } if ( cp < 0 ) { low = i + 1 ; } else { high = i - 1 ; } } return ~ low ; } param source param item param compareFn export function binaryInsert < T > ( source , item , compareFn ) { const pos = binarySearch ( source , item , compareFn ) ; if ( pos >= 0 ) { source . splice ( pos , 0 , item ) ; } else { source . splice ( ~ pos , 0 , item ) ; } }