If I add events, save, reload, remove all the events, save, reload, the old track events are back.

You could probably have an else block here and if there are no events, just save without changing the duration.

Also, might as well check to see if byEnd[ byEnd.length - 2 ].end is already === duration, if so, don't bother changing it and just save (go into the else I mentioned above)