I am trying to optimize a function which does binary search of strings in JavaScript.
Binary search requires you to know whether the key is == the pivot or < the pivot.
But this requires two string comparisons in JavaScript, unlike in C like languages which have the strcmp() function that returns three values (-1, 0, +1) for (less than, equal, greater than).
Is there such a native function in JavaScript, that can return a ternary value so that just one comparison is required in each iteration of the binary search?
return str1 < str2 ? -1 : str1 > str2;?localeCompare()on my machine. @Gumbo's customstrcmp()may be faster, depending on how optimized the internal implementation of equality comparisions for strings is.