diff --git a/module/apps/ArtistBrowser.mjs b/module/apps/ArtistBrowser.mjs index 29ffc19..6d9ee60 100644 --- a/module/apps/ArtistBrowser.mjs +++ b/module/apps/ArtistBrowser.mjs @@ -152,6 +152,24 @@ export class ArtistBrowser extends HandlebarsApplicationMixin(ApplicationV2) { const allArtists = Object.entries(deepClone(this.#artistDB.data)); const allImages = Object.values(this.#imagesDB.data); + /* + This collates all of the image data into the required summary data for the + display of the artists. Because this does the collation all in one iteration + it is a more performant way of collecting all of the information once the + databases get larger + */ + const summary = {}; + for (const image of allImages) { + for (const artistID of image.artists) { + summary[artistID] ??= { count: 0, tags: {} }; + summary[artistID].count++; + for (const tag of image.tags) { + summary[artistID].tags[tag] ??= { name: tag, count: 0 }; + summary[artistID].tags[tag].count++; + }; + }; + }; + const artists = []; for (const [id, artist] of allArtists) { // Check if it matches the required filters @@ -162,20 +180,8 @@ export class ArtistBrowser extends HandlebarsApplicationMixin(ApplicationV2) { // Populate ephemeral data for rendering artist.id = id; - let imageCount = 0; - let tags = {}; - - for (const image of allImages) { - if (!image.artists.includes(id)) continue; - imageCount++; - for (const tag of image.tags) { - tags[tag] ??= { name: tag, count: 0 }; - tags[tag].count++; - }; - }; - - artist.imageCount = imageCount; - artist.commonTags = Object.values(tags) + artist.imageCount = summary[id].count; + artist.commonTags = Object.values(summary[id].tags) .sort((a, b) => b.count - a.count) .slice(0, 5);