User:Mike Dillon/Scripts/timeOnload.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
// Requires: [[User:Mike Dillon/Scripts/easydom.js]]
// Requires: [[User:Mike Dillon/Scripts/toolbox.js]]
 
/* <pre><nowiki> */
 
var onloadFunctTimes = {};
var oldOnloadFuncts;
var oldWindowFuncts;
var oldRunOnloadHook = window.runOnloadHook;
window.runOnloadHook = function () {
    var tf = function (func) {
        var start = new Date();
        func();
        var end = new Date();
        return {
            start: start,
            end:   end,
            diff:  end.getTime() - start.getTime()
        };
    };
 
    var wrapOnload = function (id, func) {
        return function () {
            onloadFunctTimes[id] = tf(func);
        };
    };
 
    oldWindowFuncts = {};
    var wrapWindowFunct = function (name, alias) {
        if (!alias) alias = name;
        oldWindowFuncts[alias] = window[name];
        window[name] = wrapOnload(alias, oldWindowFuncts[alias]);
    };
 
    /*
    wrapWindowFunct('histrowinit', 'hri');
    wrapWindowFunct('unhidetzbutton', 'uhtb');
    wrapWindowFunct('tabbedprefs', 'tp');
    wrapWindowFunct('updateTooltipAccessKeys', 'utak');
    wrapWindowFunct('akeytt');
    wrapWindowFunct('scrollEditBox', 'seb');
    wrapWindowFunct('setupCheckboxShiftClick', 'scsc');
    wrapWindowFunct('sortableTables', 'st');
 
    oldOnloadFuncts = window.onloadFuncts;
    window.onloadFuncts = [];
    for (var i in oldOnloadFuncts) {
        window.onloadFuncts[i] = wrapOnload(i, oldOnloadFuncts[i]);
    }
    */
 
    var loadTime = tf(oldRunOnloadHook);
    with (easydom) {
        var createTooltip = function (lt) {
            return "Start: " + lt.start.getTime() + ", End: " + lt.end.getTime();
        };
 
        var createOnloadFunctItem = function (id) {
            return li(
                { "title": createTooltip(onloadFunctTimes[id]) },
                a({ "href": "#",
                    "onclick": function () {
                        if (oldOnloadFuncts[id]) {
                            alert(oldOnloadFuncts[id]);
                        } else if (oldWindowFuncts[id]) {
                            alert(oldWindowFuncts[id]);
                        }
                        return false;
                    }
                }, id), ": ", onloadFunctTimes[id].diff, " ms");
        };
 
        var d = div({ title: createTooltip(loadTime) },
            strong('JS load time: '), loadTime.diff + ' ms');
        var list = ul();
        for (var id in onloadFunctTimes) {
            list.appendChild(createOnloadFunctItem(id));
        }
        if (list.childNodes.length) {
            d.appendChild(list);
        }
 
        addToolboxLink(null, d);
    }
};
 
/* </nowiki></pre> */