jashkenas/underscore — pull request #1258 by theshadow7 (2013-08-14 06:42:24+00:00)
Title: Add _.maxmin(collection)

Hi guys,

Whenever I use _.max I am almost always trying to get the range of my collection and end up using _.min right after it. Searching on github for usages for _.max you'll notice that often a call to _.max is followed by a call to _.min. When people are trying to get the range of their collection, making separate calls to _.max and _.min is needlessly expensive. My pull request adds a maxmin function

````
var range = _.maxmin([1,2,3,4,5]);
console.log(range.max);
console.log(range.min);
````

This is similar to the minmax method in Ruby's Enumerable module except our result is returned as an object not an array. I've also written a quick benchmark to compare the performance of maxmin vs calling max and later min https://github.com/theshadow7/underscore_max_min_benchmark (using an array size of 250000 and 75 iterations)

_.max: 892 (ms)
_.min: 878 (ms)
_.maxmin: 936 (ms)
_.max + _.min: 1794 (ms)

As you can see _.maxmin is only slightly more expensive than calling _.max or _.min but almost twice as fast as successive calls to _.max and _.min. I feel this is a common enough use case with good performance upside so I'm submitting a pull request for this. I've also included unit tests. Let me know if this is reasonable

Thanks!