From 8808f7d531733a0e88d31f122ffc3568d1dfce96 Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Tue, 21 Feb 2012 10:35:55 -0700 Subject: [PATCH] Un-golf `_.uniq()`. --- underscore.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/underscore.js b/underscore.js index 9b366cb5f..88da6e1d8 100644 --- a/underscore.js +++ b/underscore.js @@ -371,9 +371,12 @@ // been sorted, you have the option of using a faster algorithm. // Aliased as `unique`. _.uniq = _.unique = function(array, isSorted, iterator) { + var initial = iterator ? _.map(array, iterator) : array; var results = []; - _.reduce(iterator ? _.map(array, iterator) : array, function (memo, value, index) { - if (array.length < 3 || isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) { + // The `isSorted` flag is irrelevant if the array only contains two elements. + if (array.length < 3) isSorted = true; + _.reduce(initial, function (memo, value, index) { + if (isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) { memo.push(value); results.push(array[index]); }