How do you performance test JavaScript code?

前端 未结 22 1132
难免孤独
难免孤独 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
    
    0 讨论(0)
  • 2020-11-22 04:38

    I usually just test javascript performance, how long script runs. jQuery Lover gave a good article link for testing javascript code performance, but the article only shows how to test how long your javascript code runs. I would also recommend reading article called "5 tips on improving your jQuery code while working with huge data sets".

    0 讨论(0)
  • 2020-11-22 04:39

    Try jsPerf. It's an online javascript performance tool for benchmarking and comparing snippets of code. I use it all the time.

    0 讨论(0)
  • 2020-11-22 04:39

    Most browsers are now implementing high resolution timing in performance.now(). It's superior to new Date() for performance testing because it operates independently from the system clock.

    Usage

    var start = performance.now();
    
    // code being timed...
    
    var duration = performance.now() - start;
    

    References

    • https://developer.mozilla.org/en-US/docs/Web/API/Performance.now()
    • http://www.w3.org/TR/hr-time/#dom-performance-now
    0 讨论(0)
  • 2020-11-22 04:43

    JSLitmus is a lightweight tool for creating ad-hoc JavaScript benchmark tests

    Let examine the performance between function expression and function constructor:

    <script src="JSLitmus.js"></script>
    <script>
    
    JSLitmus.test("new Function ... ", function() { 
        return new Function("for(var i=0; i<100; i++) {}"); 
    });
    
    JSLitmus.test("function() ...", function() { 
           return (function() { for(var i=0; i<100; i++) {}  });
    });
    
    </script>
    

    What I did above is create a function expression and function constructor performing same operation. The result is as follows:

    FireFox Performance Result

    FireFox Performance Result

    IE Performance Result

    IE Performance Result

    0 讨论(0)
  • 2020-11-22 04:45

    We can always measure time taken by any function by simple date object.

    var start = +new Date();  // log start timestamp
    function1();
    var end =  +new Date();  // log end timestamp
    var diff = end - start;
    
    0 讨论(0)
提交回复
热议问题