Youtube Music fix performance

Try to fix the performance issues that youtube music has

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Youtube Music fix performance
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Try to fix the performance issues that youtube music has
// @author       Marco Pfeiffer <[email protected]>
// @match        https://music.youtube.com/*
// @icon         https://music.youtube.com/favicon.ico
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const global = window.unsafeWindow ?? window;
    const setInterval = global.setInterval;
    const clearInterval = global.clearInterval;
    const intervals = new Set();

    global.setInterval = function(fn, ms) {
        const n = setInterval.call(this, fn, ms);
        console.log("setup interval", n, fn, ms, "still pending timers", Array.from(intervals));
        intervals.add(n);
        return n;
    };
    global.clearInterval = function (n) {
        if (!intervals.delete(n)) {
            console.log("failed cleanup of", n, "still pending timers", Array.from(intervals));
            if (intervals.size > 5) {
                const timersToCleanUp = Array.from(intervals).slice(0, -5);
                console.log("cleanup timers", timersToCleanUp);
                timersToCleanUp.forEach(clearInterval);
            }
        } else {
            console.log("cleanup interval", n, "still pending timers", Array.from(intervals));
        }
        return clearInterval.call(this, n);
    };
})();