-
-
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
Combine _.min and _.max #2142
base: master
Are you sure you want to change the base?
Combine _.min and _.max #2142
Conversation
We really gotta fix those |
10fd3a0
to
3988a67
Compare
It'd probably be good. Want to do that afterwards? |
Hmm, I can get speeds to roughly the same levels, but brings back some duplication.
var createExtremumFinder = function(dir) {
return function(obj, iteratee, context) {
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
result = dir * Infinity,
lastComputed = result,
index = 0, currentKey, value;
if (iteratee == null) {
for (; index < length; index++) {
currentKey = keys ? keys[index] : index;
value = obj[currentKey];
if (dir < 0 ? result > value : result < value) {
result = value;
}
}
} else {
for (; index < length; index++) {
currentKey = keys ? keys[index] : index;
value = obj[currentKey];
var computed = iteratee(value, currentKey, obj);
if ((dir < 0 ? computed > lastComputed : computed < lastComputed) || index === 0 && computed === lastComputed) {
result = value;
lastComputed = computed;
}
}
}
return result;
};
}; |
3158197
to
80a26bb
Compare
var value = obj[currentKey]; | ||
var computed = iteratee ? iteratee(value, currentKey, obj) : value; | ||
if ((max ? computed > lastComputed : computed < lastComputed) || (!found && computed === result)) { | ||
found = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the found variable necessary? Can't you just infer it based on index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the iteratee can return anything. So, if the first iteration returns undefined
, that won't be greater than -Infinity
. But, the next iteration might be equal.
80a26bb
to
d7ca1c1
Compare
d7ca1c1
to
b782059
Compare
Its faster for the |
Rebase for a clean merge? |
Edit: ha, wrong PR. |
http://jsperf.com/min-max-code-sharing
There's a hit on arrays without an iteratee. Looking into speeding that up, but everything else is faster.