Function to calculate upper bound
function upperBound ( array , value ) { var low = 0 ; var high = array . length ; while ( low < high ) { var mid = parseInt ( ( low + high ) / 2 ) ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; }