Skip to content

Commit 0a40ae4

Browse files
authored
fix: optimize shuffleArray array (#500)
1 parent 6a48af1 commit 0a40ae4

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/arrays.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ export function uniqueArray<T>(array: T[]): T[] {
4949
* Перемешивает исходный массив и возвращает новый
5050
*/
5151
export function shuffleArray<T>(array: T[]): T[] {
52-
return array
53-
.map<[number, T]>((a) => [Math.random(), a])
54-
.sort((a, b) => a[0] - b[0])
55-
.map<T>((a) => a[1]);
52+
const result = array.slice();
53+
54+
for (let i = result.length - 1; i > 0; i--) {
55+
const j = Math.floor(Math.random() * (i + 1));
56+
57+
[result[i], result[j]] = [result[j], result[i]];
58+
}
59+
60+
return result;
5661
}
5762

5863
/**

0 commit comments

Comments
 (0)