Skip to content

Commit

Permalink
The 'needle' value is cached in sortedIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
spadgos committed Apr 26, 2012
1 parent e1fc1d3 commit ca0f211
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@
// an object should be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iterator) {
iterator || (iterator = _.identity);
obj = iterator(obj);
var low = 0, high = array.length;
while (low < high) {
var mid = (low + high) >> 1;
iterator(array[mid]) < iterator(obj) ? low = mid + 1 : high = mid;
iterator(array[mid]) < obj ? low = mid + 1 : high = mid;
}
return low;
};
Expand Down

0 comments on commit ca0f211

Please sign in to comment.