From 12acbddd13b46b3a3196bd8ffab5a29880f53a1c Mon Sep 17 00:00:00 2001 From: Alex Barstow Date: Mon, 20 Mar 2023 11:15:44 -0400 Subject: [PATCH] refactor: remove nested loop from removeDuplicateCuesFromTrack function (#1381) --- src/util/text-tracks.js | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/util/text-tracks.js b/src/util/text-tracks.js index 1f9562b25..cd4e74021 100644 --- a/src/util/text-tracks.js +++ b/src/util/text-tracks.js @@ -285,26 +285,16 @@ export const removeDuplicateCuesFromTrack = function(track) { return; } - for (let i = 0; i < cues.length; i++) { - const duplicates = []; - let occurrences = 0; - - for (let j = 0; j < cues.length; j++) { - if ( - cues[i].startTime === cues[j].startTime && - cues[i].endTime === cues[j].endTime && - cues[i].text === cues[j].text - ) { - occurrences++; - - if (occurrences > 1) { - duplicates.push(cues[j]); - } - } - } + const uniqueCues = {}; - if (duplicates.length) { - duplicates.forEach(dupe => track.removeCue(dupe)); + for (let i = cues.length - 1; i >= 0; i--) { + const cue = cues[i]; + const cueKey = `${cue.startTime}-${cue.endTime}-${cue.text}`; + + if (uniqueCues[cueKey]) { + track.removeCue(cue); + } else { + uniqueCues[cueKey] = cue; } } };