How do you performance test JavaScript code?

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

    I find execution time to be the best measure.

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

    Profilers are definitely a good way to get numbers, but in my experience, perceived performance is all that matters to the user/client. For example, we had a project with an Ext accordion that expanded to show some data and then a few nested Ext grids. Everything was actually rendering pretty fast, no single operation took a long time, there was just a lot of information being rendered all at once, so it felt slow to the user.

    We 'fixed' this, not by switching to a faster component, or optimizing some method, but by rendering the data first, then rendering the grids with a setTimeout. So, the information appeared first, then the grids would pop into place a second later. Overall, it took slightly more processing time to do it that way, but to the user, the perceived performance was improved.


    These days, the Chrome profiler and other tools are universally available and easy to use, as are console.time(), console.profile(), and performance.now(). Chrome also gives you a timeline view which can show you what is killing your frame rate, where the user might be waiting, etc.

    Finding documentation for all these tools is really easy, you don't need an SO answer for that. 7 years later, I'll still repeat the advice of my original answer and point out that you can have slow code run forever where a user won't notice it, and pretty fast code running where they do, and they will complain about the pretty fast code not being fast enough. Or that your request to your server API took 220ms. Or something else like that. The point remains that if you take a profiler out and go looking for work to do, you will find it, but it may not be the work your users need.

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

    I think JavaScript performance (time) testing is quite enough. I found a very handy article about JavaScript performance testing here.

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

    Performance testing became something of a buzzword as of late but that’s not to say that performance testing is not an important process in QA or even after the product has shipped. And while I develop the app I use many different tools, some of them mentioned above like the chrome Profiler I usually look at a SaaS or something opensource that I can get going and forget about it until I get that alert saying that something went belly up.

    There are lots of awesome tools that will help you keep an eye on performance without having you jump through hoops just to get some basics alerts set up. Here are a few that I think are worth checking out for yourself.

    1. Sematext.com
    2. Datadog.com
    3. Uptime.com
    4. Smartbear.com
    5. Solarwinds.com

    To try and paint a clearer picture, here is a little tutorial on how to set up monitoring for a react application.

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

    This is a good way of collecting performance information for the specific operation.

    start = new Date().getTime(); 
    for (var n = 0; n < maxCount; n++) {
    /* perform the operation to be measured *//
    }
    elapsed = new Date().getTime() - start;
    assert(true,"Measured time: " + elapsed);
    
    0 讨论(0)
  • 2020-11-22 04:53

    You could use https://github.com/anywhichway/benchtest which wraps existing Mocha unit tests with performance tests.

    0 讨论(0)
提交回复
热议问题