From a40001a358a185b4da8a2836381ce5a5a5d29eb6 Mon Sep 17 00:00:00 2001 From: Jakub Wieczorek Date: Tue, 3 Apr 2012 15:23:38 +0200 Subject: [PATCH] Remove the unnecessary branch in _.shuffle() The non-zero case works properly for 0 as well. --- test/speed.js | 4 ++++ underscore.js | 10 +++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/speed.js b/test/speed.js index 86663a237..82033c51f 100644 --- a/test/speed.js +++ b/test/speed.js @@ -67,4 +67,8 @@ return _.range(1000); }); + JSLitmus.test('_.shuffle()', function() { + return _.shuffle(numbers); + }); + })(); \ No newline at end of file diff --git a/underscore.js b/underscore.js index 72dc8e9f7..3b6e4b990 100644 --- a/underscore.js +++ b/underscore.js @@ -250,13 +250,9 @@ _.shuffle = function(obj) { var shuffled = [], rand; each(obj, function(value, index, list) { - if (index == 0) { - shuffled[0] = value; - } else { - rand = Math.floor(Math.random() * (index + 1)); - shuffled[index] = shuffled[rand]; - shuffled[rand] = value; - } + rand = Math.floor(Math.random() * (index + 1)); + shuffled[index] = shuffled[rand]; + shuffled[rand] = value; }); return shuffled; };