From 08a0742ce76003a7b2468cdbd648709b3ac2cda3 Mon Sep 17 00:00:00 2001 From: Graeme Yeates Date: Thu, 3 Oct 2019 17:22:09 -0400 Subject: [PATCH] (4.17) Short circuit sortedIndexBy methods for empty arrays --- lodash.js | 11 +++++++---- test/test.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index 0770ff5671..75868f2145 100644 --- a/lodash.js +++ b/lodash.js @@ -4129,11 +4129,14 @@ * into `array`. */ function baseSortedIndexBy(array, value, iteratee, retHighest) { - value = iteratee(value); - var low = 0, - high = array == null ? 0 : array.length, - valIsNaN = value !== value, + high = array == null ? 0 : array.length; + if (high === 0) { + return 0; + } + + value = iteratee(value); + var valIsNaN = value !== value, valIsNull = value === null, valIsSymbol = isSymbol(value), valIsUndefined = value === undefined; diff --git a/test/test.js b/test/test.js index 72c71a9936..b6c3533107 100644 --- a/test/test.js +++ b/test/test.js @@ -20998,6 +20998,16 @@ assert.strictEqual(actual, 1); }); + QUnit.test('`_.' + methodName + '` should avoid calling iteratee when length is 0', function(assert) { + var objects = [], + iteratee = function() { + throw new Error; + }, + actual = func(objects, { 'x': 50 }, iteratee); + + assert.strictEqual(actual, 0); + }); + QUnit.test('`_.' + methodName + '` should support arrays larger than `MAX_ARRAY_LENGTH / 2`', function(assert) { assert.expect(12);