How can I benchmark JavaScript code? [closed]

Is there a package that helps me benchmark JavaScript code? I'm not referring to Firebug and such tools.

I need to compare 2 different JavaScript functions that I have implemented. I'm very familiar with Perl's Benchmark (Benchmark.pm) module and I'm looking for something similar in JavaScript.

Has the emphasis on benchmarking JavaScript code gone overboard? Can I get away with timing just one run of the functions?


jsperf.com is the go-to site for testing JS performance. Start there. If you need a framework for running your own tests from the command line or scripts use Benchmark.js, the library upon which jsperf.com is built.

Note: Anyone testing Javascript code should educate themselves on the pitfalls of "microbenchmarks" (small tests that target a specific feature or operation, rather than more complex tests based on real-world code patterns). Such tests can be useful but are prone to inaccuracy due to how modern JS runtimes operate. Vyacheslav Egorov's presentation on performance and benchmarking is worth watching to get a feel for the nature of the problem(s).

Edit: Removed references to my JSLitmus work as it's just no longer relevant or useful.


Just simple way.

console.time('test');
console.timeEnd('test');

Just adding a quick timer to the mix, which someone may find useful:

var timer = function(name) {
    var start = new Date();
    return {
        stop: function() {
            var end  = new Date();
            var time = end.getTime() - start.getTime();
            console.log('Timer:', name, 'finished in', time, 'ms');
        }
    }
};

Ideally it would be placed in a class, and not used as a global like I did for example purposes above. Using it would be pretty simple:

var t = timer('Some label');
// code to benchmark
t.stop(); // prints the time elapsed to the js console