From 79a7b4d1c337e06a8c0a61371e0fcf84beeec1c9 Mon Sep 17 00:00:00 2001 From: ScottDowne Date: Wed, 11 May 2011 13:33:40 -0400 Subject: [PATCH] [#t483] performance work for addTrackEvent --- popcorn.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/popcorn.js b/popcorn.js index 0e4debd..4d1473a 100644 --- a/popcorn.js +++ b/popcorn.js @@ -577,14 +577,24 @@ track.end = Popcorn.util.toSeconds( track.end, obj.options.framerate ); // Store this definition in an array sorted by times - obj.data.trackEvents.byStart.push( track ); - obj.data.trackEvents.byEnd.push( track ); - obj.data.trackEvents.byStart.sort( function( a, b ){ - return ( a.start - b.start ); - }); - obj.data.trackEvents.byEnd.sort( function( a, b ){ - return ( a.end - b.end ); - }); + var byStart = obj.data.trackEvents.byStart, + byEnd = obj.data.trackEvents.byEnd; + + for (var idx = byStart.length-1; idx >= 0; idx--) { + + if (track.start >= byStart[idx].start) { + obj.data.trackEvents.byStart.splice(idx+1, 0, track); + break; + } + } + + for (var idx = byEnd.length-1; idx >= 0; idx--) { + + if (track.start >= byEnd[idx].start) { + obj.data.trackEvents.byEnd.splice(idx+1, 0, track); + break; + } + } }; -- 2.43.0