Skip to main content

howto benchmark javascript code?



Is there a package that helps me benchmark JS code ? Im not referring the Firebug and such tools.





I need to compare 2 different JS functions that I have implemented. Im very familiar with perl's Benchmark ( http://search.cpan.org/~tty/kurila-1.19_0/lib/Benchmark.pm ) module and Im looking for something similar in javascript.





Is the emphasis on benchmarking the JS code overboard ? Can I get away with timing just one run of the functions ?



Source: Tips4all

Comments

  1. Just time several iterations of each function. One iteration probably won't be enough, but (depending on how complex your functions are) somewhere closer to 100 or even 1,000 iterations should do the job.

    Firebug also has a profiler if you want to see which parts of your function are slowing it down.

    ReplyDelete
  2. I'd start with http://jsperf.com . It works well for testing code that can easily be copy-pasted into the UI there.

    If you need more control over the setup (i.e. you want to provide lots of markup and/or import scripts), you can try my JSLitmus script:
    http://www.broofa.com/Tools/JSLitmus/

    The latest version (designed to provide more API and less UI) can be found here:
    https://github.com/broofa/jslitmus

    ReplyDelete
  3. I have been using this simple implementation of @musicfreaks answer. There are no features, but it is really easy to use. This bench(function(){return 1/2;}, 10000, [], this) will calculate 1/2 10,000 times.

    /**
    * Figure out how long it takes for a method to execute.
    *
    * @param {func} method to test
    * @param {int} iterations number of executions.
    * @param {Array} args to pass in.
    * @param {T} context the context to call the method in.
    * @return {int} the time it took, in milliseconds to execute.
    */
    var bench = function (method, iterations, args, context) {

    var time = 0;
    var timer = function (action) {
    var d = +(new Date);
    if (time < 1 || action === 'start') {
    time = d;
    return 0;
    } else if (action === 'stop') {
    var t = d - time;
    time = 0;
    return t;
    } else {
    return d - time;
    }
    };

    var result = [];
    var i = 0;
    timer('start');
    while (i < iterations) {
    result.push(method.apply(context, args));
    i++;
    }

    var execTime = timer('stop');

    if ( typeof console === "object") {
    console.log("Mean execution time was: ", execTime / iterations);
    console.log("Sum execution time was: ", execTime);
    console.log("Result of the method call was:", result[0]);
    }

    return execTime;
    };

    ReplyDelete
  4. It’s really hard to write decent cross-browser benchmarks. Simply timing a pre-defined number of iterations of your code is not bulletproof at all.

    As @broofa already suggested, check out jsPerf. It uses Benchmark.js behind the scenes.

    ReplyDelete
  5. if writing a custom benchmark script be sure to note that some browsers apply dom manipulations only after function in which they are defined is ended. More details here
    http://www.quirksmode.org/blog/archives/2009/08/when_to_read_ou.html

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex