How can I compare JavaScript code?

Is there a package that helps me test JavaScript code? I do not mean Firebug and such tools.

I need to compare two different JavaScript functions that I implemented. I am very familiar with the Perl Benchmark module ( Benchmark.pm ) and I am looking for something similar in JavaScript.

Does the focus on benchmarking JavaScript code overboard? Can I get away with only one function run over time?

+85
javascript benchmarking
Jun 16 '09 at 20:46
source share
8 answers

Just a few iterations of each function. One iteration will probably not be enough, but (depending on how complex your functions are), somewhere closer to 100 or even thousands of iterations, the task should be done.

Firebug also has a profiler if you want to see which parts of your function slow it down. / p>

Edit: For future readers, the correct answer recommending JSPerf should be the correct answer. I would delete mine, but I cannot, because it was selected by OP. There is much more to benchmarking than just running many iterations, and JSPerf will take care of this for you.

+25
Jun 16 '09 at 20:50
source share

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

Note. . Any Javascript testing code should educate itself in the pitfalls of "microobjects" (small tests designed for a specific function or operation, and not more complex tests based on real code samples). Such tests can be useful, but they are prone to inaccuracies due to how modern JS environments work. Vyacheslav Egorov's introduction on performance and comparative testing is worth a look to understand the nature of the problem (s).

Edit: Removed links to my JSLitmus work as it is no longer relevant or useful.

+101
Nov 29 2018-10-11T00:
source share

Just add a quick timer to the mix that may be 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 will be placed in a class and will not be used as global, as I did, for example, for the 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 
+55
Sep 16 '14 at 8:45
source share

I use this simple implementation of @musicfreaks answer. There are no functions, but it is very easy to use. This bench(function(){return 1/2;}, 10000, [], this) will calculate 1/2 10000 times.

 /** * Figure out how long it takes for a method to execute. * * @param {Function} method to test * @param {number} iterations number of executions. * @param {Array} args to pass in. * @param {T} context the context to call the method in. * @return {number} the time it took, in milliseconds to execute. */ var bench = function (method, iterations, args, context) { var time = 0; var timer = function (action) { var d = Date.now(); 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; }; 
+19
Jan 10 2018-11-11T00:
source share

Just an easy way.

 console.time('test'); console.timeEnd('test'); 
+15
09 Oct '17 at 15:16
source share

It's actually hard to write decent cross-browser tests. Just setting a certain number of iterations of your code is not bulletproof at all .

As @broofa has already suggested, check out jsPerf . He uses Benchmark.js backstage.

+7
Feb 16 '11 at 11:25
source share

when writing a user test script, be sure to note that some browsers only use dom manipulations after the function in which they are defined is completed. More details here http://www.quirksmode.org/blog/archives/2009/08/when_to_read_ou.html

+2
Sep 14 '11 at 19:07
source share

If you need something simple, you can do the following:

 'use strict' console.clear() const powerOf = x => y => Math.pow(x, y) const powerOfThree = powerOf(3) function performanceCalc(fn, ...params) { const start = +new Date() const result = fn(...params) const end = +new Date() console.log(`Result: ${result}. Execution Time: ${end - start} ms`) } performanceCalc(powerOfThree, 2) 

Here is a sample code

+1
Aug 04 '16 at 0:57
source share



All Articles