Skip to content

Commit

Permalink
Improve album shuffle efficiency
Browse files Browse the repository at this point in the history
Just generate a random number and increase it if its larger or equal to the current index, instead of shuffling the album.
O(1) instead of O(N)
  • Loading branch information
AgustinSRG committed Aug 14, 2024
1 parent 4fa8786 commit b0e2cb3
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions frontend/src/control/albums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"use strict";

import { makeNamedApiRequest, abortNamedApiRequest, makeApiRequest } from "@asanrom/request-browser";
import { shuffleArray } from "@/utils/shuffle";
import { setNamedTimeout, clearNamedTimeout } from "@/utils/named-timeouts";
import { AppEvents } from "./app-events";
import { AppStatus, EVENT_NAME_APP_STATUS_CHANGED } from "./app-status";
Expand Down Expand Up @@ -422,12 +421,14 @@ export class AlbumsController {

if (mediaPos >= 0) {
if (AlbumsController.AlbumRandom) {
const shuffled = shuffleArray(AlbumsController.CurrentAlbumData.list).filter((a) => {
return a.id !== mediaId;
});
let randomIndex = Math.floor(Math.random() * (AlbumsController.CurrentAlbumData.list.length - 1));

if (randomIndex >= mediaPos) {
randomIndex++;
}

AlbumsController.CurrentPrev = null;
AlbumsController.CurrentNext = shuffled[1] || null;
AlbumsController.CurrentNext = AlbumsController.CurrentAlbumData.list[randomIndex] || null;

if (AlbumsController.AlbumLoop) {
if (AlbumsController.CurrentNext === null) {
Expand Down

0 comments on commit b0e2cb3

Please sign in to comment.