« get me outta code hell

simple findTrackObject opimizations - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-07-09 14:50:06 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-16 18:28:07 -0300
commit3f17023373f3fa26ccb556ab1f83215b2a134e8c (patch)
tree6e057898289d4ede1ae45965e84341380910ae35
parentb6d86d7baf65b4a66ad767c73c89c793d83083f0 (diff)
simple findTrackObject opimizations
-rw-r--r--playlist-utils.js22
1 files 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)
   }))