From 3f17023373f3fa26ccb556ab1f83215b2a134e8c Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 9 Jul 2020 14:50:06 -0300 Subject: simple findTrackObject opimizations --- playlist-utils.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/playlist-utils.js b/playlist-utils.js index 739652f..f58a9e8 100644 --- a/playlist-utils.js +++ b/playlist-utils.js @@ -847,12 +847,15 @@ export function getPathScore(path1, path2) { return scores.reduce((a, b) => a < b ? a : b) } -export function findTrackObject(referenceData, sourcePlaylist) { +export function findTrackObject(referenceData, sourcePlaylist, flattenedSourcePlaylist = null) { // Finds the track object in the source playlist which most closely resembles // the provided reference data. This is used for maintaining the identity of // track objects when reloading a playlist (see serialized-backend.js). It's // also usable in synchronizing the identity of tracks across linked clients // (see socket.js). + // + // NB: This function is many times more efficient if you pass a preemptively + // flattened version of the source playlist in as well! // Reference data includes track NAME, track SOURCE (downloaderArg), and // track PATH (names of parent groups). Specifics of how existing track @@ -875,18 +878,15 @@ export function findTrackObject(referenceData, sourcePlaylist) { return getPathScore(path1, path2) } - function compareName(track) { - return track.name === referenceData.name - } - - function compareSource(track) { - return track.downloaderArg === referenceData.downloaderArg + if (!flattenedSourcePlaylist) { + flattenedSourcePlaylist = flattenGrouplike(sourcePlaylist) } // The only tracks which will be considered at all are those which match at // least one of the reference name/source. - const baselineResemble = flattenGrouplike(sourcePlaylist).items.filter( - track => compareName(track) || compareSource(track)) + const baselineResemble = flattenedSourcePlaylist.items.filter(track => + track.name === referenceData.name || + track.downloaderArg === referenceData.downloaderArg) // If no track matches the baseline conditions for resemblance at all, // return null. It's up to the caller to decide what to do in this case, @@ -900,8 +900,8 @@ export function findTrackObject(referenceData, sourcePlaylist) { // be used as the factors in calculating which track resembles closest. const reasons = baselineResemble.map(track => ({ track, - nameMatches: compareName(track), - sourceMatches: compareSource(track), + nameMatches: track.name === referenceData.name, + sourceMatches: track.downloaderArg === referenceData.downloaderArg, pathScore: getTrackPathScore(track) })) -- cgit 1.3.0-6-gf8a5