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; };