How to get the output from console.timeEnd() in JS Console?

后端 未结 4 2093
感情败类
感情败类 2021-01-31 07:50

I\'d like to be able to get the string returned from console.timeEnd(\'t\') in my Google Chrome Javascript Console.

In this example below, I\'d like one var

4条回答
  •  情书的邮戳
    2021-01-31 08:11

    Small helper to do some time measuring. timerEnd returns the time in ms, also the timers object contains information about how many times the timer with this name was used, the sum of all measured times and the average of the measurements. I find this quite useful, since the measured time for an operation depends on many factors, so it's best to measure it several times and look at the average.

    var timers = {};
    function timer(name) {
        timers[name + '_start'] = window.performance.now();
    }
    
    function timerEnd(name) {
        if (!timers[name + '_start']) return undefined;
        var time = window.performance.now() - timers[name + '_start'];
        var amount = timers[name + '_amount'] = timers[name + '_amount'] ? timers[name + '_amount'] + 1 : 1;
        var sum = timers[name + '_sum'] = timers[name + '_sum'] ? timers[name + '_sum'] + time : time;
        timers[name + '_avg'] = sum / amount;
        delete timers[name + '_start'];
        return time;
    }
    

提交回复
热议问题