« 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
path: root/playlist-utils.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-07-09 14:50:06 -0300
committerFlorrie <towerofnix@gmail.com>2020-07-09 16:18:46 -0300
commitb07f167f86106200ed5ae4ce545c364490d4f397 (patch)
tree6b84e31ba8388161b1701a9a51ee91f92a092fdc /playlist-utils.js
parentbb6a25151e44c524cd463df52a7bdb2a4e0eda67 (diff)
simple findTrackObject opimizations
Diffstat (limited to 'playlist-utils.js')
-rw-r--r--playlist-utils.js22
1 files 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)
   }))