diff --git a/.internal/baseSortedIndexBy.js b/.internal/baseSortedIndexBy.js index fdc5c17bbb..4f3f84913e 100644 --- a/.internal/baseSortedIndexBy.js +++ b/.internal/baseSortedIndexBy.js @@ -18,10 +18,14 @@ const MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1 * into `array`. */ function baseSortedIndexBy(array, value, iteratee, retHighest) { - value = iteratee(value) - let low = 0 let high = array == null ? 0 : array.length + if (high == 0) { + return 0 + } + + value = iteratee(value) + const valIsNaN = value !== value const valIsNull = value === null const valIsSymbol = isSymbol(value) diff --git a/test/sortedIndexBy-methods.js b/test/sortedIndexBy-methods.js index 25f96f3b48..ae3b5feaa7 100644 --- a/test/sortedIndexBy-methods.js +++ b/test/sortedIndexBy-methods.js @@ -24,6 +24,13 @@ describe('sortedIndexBy methods', function() { assert.strictEqual(actual, 1); }); + it('`_.' + methodName + '` should avoid calling iteratee when length is 0', function() { + var objects = [], + actual = func(objects, { 'x': 50 }, assert.fail); + + assert.strictEqual(actual, 0); + }); + it('`_.' + methodName + '` should support arrays larger than `MAX_ARRAY_LENGTH / 2`', function() { lodashStable.each([Math.ceil(MAX_ARRAY_LENGTH / 2), MAX_ARRAY_LENGTH], function(length) { var array = [],