How do you performance test JavaScript code?

前端 未结 22 1136
难免孤独
难免孤独 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: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

提交回复
热议问题