-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
_.sortedFind to go with _.sortedIndex #68
Comments
Hmm. Do you really need Underscore to add a function for this?
|
Well really it would be more like...
The point isn't that it's difficult to implement but rather that it feels incomplete to have a function which inserts into an array in-order but no function to query for a location of an object in said array while taking advantage of its sortedness. As far as I can think of, there's only two uses for sortedIndex: Keeping a sorted list for display-purposes (simple iteration), and for maintaining an efficient datastructure. My request is for the latter scenario. If there's no interest in supporting that, then I'll maintain my own helpers. :) |
I'm sorry -- I misunderstood the proposal. I now understand that this is an optimization of |
Sorry, I didn't think there was any doubt that an O(logn) binary search performs better than an O(n) full scan. Here's a benchmark, inserts a sorted array and tries to find each element in the array plus same number of elements that don't exist in the array:
And the results on my MBP:
|
Thanks -- it's been a bit of a policy at this point to ask for benchmarks before starting on an optimization expansion to the API, regardless of the underlying idea. One more question: do you think it's problematic that, if the sorted array contains more than one copy of the value you're looking for, you'll find the index of a random copy of the value? Adding an unrelated element to the end of the array could change the index returned, the next time you call |
The algorithm could be altered to always return the first such element in the array with minimal impact to the performance to match the behaviour of the native indexOf (just be careful not to fall back into O(n) for cases when the array is full of the same elements), and since JavaScript 1.6 there is also a lastIndexOf if that's something you're interested in replicating. In my use cases, this doesn't affect me, but I can see an upside to replicating the behaviour of the native indexOf (even at the cost of a few extra comparisons). Edit: If it were up to me, I'd leave the implementation as is (returning whatever index as long as it matches the element) until there is demand for a different behaviour. Perhaps put a note in the docs. |
Also I think I accidentally closed the ticket, my bad. |
Sure thing -- reopened the ticket. |
Alright -- I've added it as Thanks for tolerating my denseness earlier in the ticket -- I don't know what I was thinking ;) |
Whoops, nevermind. Sam Clay reminded me that since the API is identical, this should really be a flag passed to |
Thank you. :) |
Function request:
A _.sortedFind function to go with _.sortedIndex (or perhaps call it _.sortedIndexOf), which would behave similarly to the Array's native indexOf except it would assume it's a sorted array thus using binary search.
_.sortedFind(array, obj, ...)
would return the index position of the first occurrence of obj within array, else -1 if not found.The code to implement this is almost identical to _.sortedIndex (and therefore could use it) except it would return a -1 if the object is not found.
The text was updated successfully, but these errors were encountered: