Skip to content
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

Add _.comparator and align sortedIndex to sortBy #1939

Merged
merged 1 commit into from
Dec 29, 2014

Conversation

megawac
Copy link
Collaborator

@megawac megawac commented Nov 17, 2014

Resolves #1768 and fixes #1834

This could use some review and thoughts

@@ -394,7 +388,7 @@
var low = 0, high = array.length;
while (low < high) {
var mid = low + high >>> 1;
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
if (_.comparitor(value, iteratee(array[mid])) === 1) low = mid + 1; else high = mid;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LMK if stylistically you prefer above or

   if (_.comparitor(iteratee(array[mid]), value) < 0) low = mid + 1; else high = mid;

@michaelficarra
Copy link
Collaborator

It's spelled "comparator".

@megawac megawac changed the title Add _.comparitor and align sortedIndex to sortBy Add _.comparator and align sortedIndex to sortBy Nov 17, 2014
@jashkenas
Copy link
Owner

Is the idea that this is something we should be exposing as a public method? If so, what's the use case?

If not, let's make it an internal function.

@michaelficarra
Copy link
Collaborator

Why is this exposed? I'd prefer we not expose it.

edit: I didn't refresh the page, and crossed paths with @jashkenas.

@megawac
Copy link
Collaborator Author

megawac commented Nov 17, 2014

@jashkenas @michaelficarra I exposed it because of #1768 (comment)

Also allows some sanity checking unit tests which will be inheritted by sortBy and sortedIndex

@megawac
Copy link
Collaborator Author

megawac commented Dec 29, 2014

Can we get this in/reviewed? I'll rebase this in a minute

@michaelficarra
Copy link
Collaborator

LGTM.

megawac added a commit that referenced this pull request Dec 29, 2014
Add _.comparator and align sortedIndex to sortBy
@megawac megawac merged commit 5f52e5b into jashkenas:master Dec 29, 2014
@megawac megawac deleted the comparitors branch December 29, 2014 23:38
@jdalton
Copy link
Contributor

jdalton commented Dec 30, 2014

How does this affect the performance of _.sortedIndex, _.max, & _.min?
I have a hunch it's a substantial hit.

Also is comparator the best name?
It shows up in Java docs, is there any other language precedents for a related name?

Reducing duplication and exposing a comparator function is nice, but not critical, so IMO it's not worth a substantial loss in perf, clarity, or anything else really.

@megawac
Copy link
Collaborator Author

megawac commented Dec 30, 2014

I think whatever the hit on sortedIndex is fine for consistency as its doing a binary search anyway, but I agree #1995 definitely needs to be benchmarked

As for the name I'm not partial to comparator, but its definition is spot on for whats happening

@jdalton
Copy link
Contributor

jdalton commented Dec 30, 2014

I think whatever the hit on sortedIndex is fine for consistency as its doing a binary search anyway, but I agree #1995 definitely needs to be benchmarked

I've worked pretty hard at maintaining the perf of sortedIndex for my implementation while making the match/sorting consistent. The offhand comment of one dev isn't enough draw for me to include it in my implementation at the moment.

The perf concern for sortedIndex is valid as while experimenting in my own branch a while back I saw some engines may actually perform worse using the binary search for things like _.indexOf than the non-binary option if _.sortedIndex performance is reduced.

@megawac
Copy link
Collaborator Author

megawac commented Dec 30, 2014

Eh, this will need another look I suppose

http://jsperf.com/sortedindex/6

@megawac
Copy link
Collaborator Author

megawac commented Dec 30, 2014

Anyway I'm a big fan of exposing comparator as it allows devs easy
customization for oddball data structures and frankly I've needed a solid
comparator in my own code in the past
On Dec 30, 2014 5:05 AM, "John-David Dalton" [email protected]
wrote:

The perf concern for sortedIndex is valid as while experimenting in my
own branch a whole back I saw some engines may actually perform worse using
the binary search for things like _.indexOf than the non-binary option if
_.sortedIndex performance is reduced.


Reply to this email directly or view it on GitHub
#1939 (comment).

@jdalton
Copy link
Contributor

jdalton commented Dec 30, 2014

Anyway I'm a big fan of exposing comparator as it allows devs easy
customization for oddball data structures and frankly I've needed a solid
comparator in my own code in the past

That's great but Underscore isn't your personal toy store. These things need a bit more hammering before introducing into core.

Answering my previous question:

How does this affect the performance of _.sortedIndex, _.max, & _.min?

Up to a 73% hit for _.sortedIndex and an 83% hit for _.max & _.min.

@jashkenas
Copy link
Owner

Right. Let's please back this out for now.

@megawac
Copy link
Collaborator Author

megawac commented Dec 30, 2014

I don't think we need to back this out. We can improve the perf of the implementation for sure and argue over whether it should be exposed or not but I think its a change for the better

@jdalton
Copy link
Contributor

jdalton commented Dec 30, 2014

I don't think we need to back this out.

Backing it out isn't the end of the road.
It's a chance to refine your implementation without dragging down core.

@braddunbar
Copy link
Collaborator

Anyway I'm a big fan of exposing comparator as it allows devs easy
customization for oddball data structures and frankly I've needed a solid
comparator in my own code in the past

Can you expand on this? Wouldn't you just return a different value or use #sort directly in those cases?

@jashkenas
Copy link
Owner

To repeat -- let's please back this out for the time being. (I don't want to forget about it.)

@megawac
Copy link
Collaborator Author

megawac commented Jan 5, 2015

@braddunbar lets say you want to to do add support to something similar to pythons class __cmp__

_.mixin({
   comparator: _.wrap(_.compatator, function(baseCompare, a, b) {
     if (_.isFunction(b.__cmp__)) return b.__cmp__(a);
     if (_.isFunction(a.__cmp__) return a.__cmp__(b);
     return baseCompare(a, b);
  })
});

@megawac megawac restored the comparitors branch January 5, 2015 18:05
megawac added a commit that referenced this pull request Jan 5, 2015
This reverts commit 5f52e5b, reversing
changes made to 9924dba.
@megawac megawac deleted the comparitors branch July 14, 2015 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

_.sortedIndex align with _.sortBy Comparator edge-case handling
5 participants