How do you performance test JavaScript code?

前端 未结 22 1097
难免孤独
难免孤独 2020-11-22 04:18

CPU Cycles, Memory Usage, Execution Time, etc.?

Added: Is there a quantitative way of testing performance in JavaScript besides just perception of how fast the code

22条回答
  •  抹茶落季
    2020-11-22 04:36

    Here is a reusable class for time performance. Example is included in code:

    /*
         Help track time lapse - tells you the time difference between each "check()" and since the "start()"
    
     */
    var TimeCapture = function () {
        var start = new Date().getTime();
        var last = start;
        var now = start;
        this.start = function () {
            start = new Date().getTime();
        };
        this.check = function (message) {
            now = (new Date().getTime());
            console.log(message, 'START:', now - start, 'LAST:', now - last);
            last = now;
        };
    };
    
    //Example:
    var time = new TimeCapture();
    //begin tracking time
    time.start();
    //...do stuff
    time.check('say something here')//look at your console for output
    //..do more stuff
    time.check('say something else')//look at your console for output
    //..do more stuff
    time.check('say something else one more time')//look at your console for output
    

提交回复
热议问题