8000 (4.17) Short circuit sortedIndexBy methods for empty arrays (#4497) · lodash/lodash@602cc3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 602cc3f

Browse files
megawacjdalton
authored andcommitted
(4.17) Short circuit sortedIndexBy methods for empty arrays (#4497)
1 parent b281dde commit 602cc3f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lodash.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,11 +4129,14 @@
41294129
* into `array`.
41304130
*/
41314131
function baseSortedIndexBy(array, value, iteratee, retHighest) {
4132-
value = iteratee(value);
4133-
41344132
var low = 0,
4135-
high = array == null ? 0 : array.length,
4136-
valIsNaN = value !== value,
4133+
high = array == null ? 0 : array.length;
4134+
if (high === 0) {
4135+
return 0;
4136+
}
4137+
4138+
value = iteratee(value);
4139+
var valIsNaN = value !== value,
41374140
valIsNull = value === null,
41384141
valIsSymbol = isSymbol(value),
41394142
valIsUndefined = value === undefined;

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20998,6 +20998,16 @@
2099820998
assert.strictEqual(actual, 1);
2099920999
});
2100021000

21001+
QUnit.test('`_.' + methodName + '` should avoid calling iteratee when length is 0', function(assert) {
21002+
var objects = [],
21003+
iteratee = function() {
21004+
throw new Error;
21005+
},
21006+
actual = func(objects, { 'x': 50 }, iteratee);
21007+
21008+
assert.strictEqual(actual, 0);
21009+
});
21010+
2100121011
QUnit.test('`_.' + methodName + '` should support arrays larger than `MAX_ARRAY_LENGTH / 2`', function(assert) {
2100221012
assert.expect(12);
2100321013

0 commit comments

Comments
 (0)
0