From b07f167f86106200ed5ae4ce545c364490d4f397 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 1413fb8..5279177 100644 --- a/playlist-utils.js +++ b/playlist-utils.js @@ -782,12 +782,15 @@ function getPathScore(path1, path2) { return scores.reduce((a, b) => a < b ? a : b) } -function findTrackObject(referenceData, sourcePlaylist) { +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 @@ -810,18 +813,15 @@ 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, @@ -835,8 +835,8 @@ 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