From 4ddc3b518bd74e9c10c2af161bbf32b62d3a3844 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 8 Jul 2014 00:36:52 -0700 Subject: [PATCH] Optimize `_.shuffle`. --- lodash.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lodash.js b/lodash.js index f76bbb7ab..b481db01f 100644 --- a/lodash.js +++ b/lodash.js @@ -5242,13 +5242,16 @@ function shuffle(collection) { collection = toIterable(collection); - var index = -1, + var index = 0, length = collection.length, result = Array(length); + if (length) { + result[index] = collection[index]; + } while (++index < length) { var value = collection[index], - rand = baseRandom(0, index); + rand = baseRandom(0, index - 1); result[index] = result[rand]; result[rand] = value;