Skip to content

Commit 37e999d

Browse files
committed
Optimise playlist shuffling
1 parent cfae8aa commit 37e999d

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/renderer/components/WatchVideoPlaylist/WatchVideoPlaylist.vue

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,21 +692,26 @@ function parseUserPlaylist(playlist) {
692692
693693
function shufflePlaylistItems() {
694694
// Prevents the array from affecting the original object
695-
const remainingItems = [].concat(playlistItems.value)
696-
const items = []
695+
const items = playlistItems.value.slice()
696+
697+
let cachedCurrentVideo
697698
698699
if (currentVideo.value != null) {
699-
items.push(currentVideo.value)
700-
remainingItems.splice(currentVideoIndexZeroBased.value, 1)
700+
cachedCurrentVideo = items.splice(currentVideoIndexZeroBased.value, 1)
701701
// There is no else case
702702
// If current video is absent in (removed from) the playlist, nothing should be changed
703703
}
704704
705-
while (remainingItems.length > 0) {
706-
const randomInt = Math.floor(Math.random() * remainingItems.length)
705+
for (let i = items.length - 1; i > 0; i--) {
706+
const j = Math.floor(Math.random() * (i + 1))
707+
708+
const temp = items[i]
709+
items[i] = items[j]
710+
items[j] = temp
711+
}
707712
708-
items.push(remainingItems[randomInt])
709-
remainingItems.splice(randomInt, 1)
713+
if (cachedCurrentVideo !== undefined) {
714+
items.unshift(cachedCurrentVideo)
710715
}
711716
712717
randomizedPlaylistItems.value = items

0 commit comments

Comments
 (0)