mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 19:07:49 +00:00
Optimize _.shuffle.
This commit is contained in:
@@ -5242,13 +5242,16 @@
|
|||||||
function shuffle(collection) {
|
function shuffle(collection) {
|
||||||
collection = toIterable(collection);
|
collection = toIterable(collection);
|
||||||
|
|
||||||
var index = -1,
|
var index = 0,
|
||||||
length = collection.length,
|
length = collection.length,
|
||||||
result = Array(length);
|
result = Array(length);
|
||||||
|
|
||||||
|
if (length) {
|
||||||
|
result[index] = collection[index];
|
||||||
|
}
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
var value = collection[index],
|
var value = collection[index],
|
||||||
rand = baseRandom(0, index);
|
rand = baseRandom(0, index - 1);
|
||||||
|
|
||||||
result[index] = result[rand];
|
result[index] = result[rand];
|
||||||
result[rand] = value;
|
result[rand] = value;
|
||||||
|
|||||||
Reference in New Issue
Block a user