Skip to content

Instantly share code, notes, and snippets.

@nijikokun
Last active March 12, 2023 12:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nijikokun/7077141 to your computer and use it in GitHub Desktop.
Save nijikokun/7077141 to your computer and use it in GitHub Desktop.
Benchmark single method execution time easily using Node.js or Javascript in general.

So, I was developing a node shell script and wanted to determine the time it took from start, to finish to generate the output of a file. Simple, right? It is, but the problem is that if you want a clean way to do it... you have to develop it, otherwise you'll have a lot of wrapper code surrounding methods and such. So I wrote a small method to simplify it even further.

Benchmark Method:

function benchmark (method) {
  var start = +(new Date);

  method && method(function (callback) {
    var end = +(new Date);
    var difference = end - start;
    callback && callback(start, end, {
      milliseconds: difference,
      ms: difference,
      seconds: (difference / 1000) % 60,
      minutes: (difference / (1000 * 60)) % 60,
      hours: (difference / (1000 * 60 * 60)) % 24
    });
  });
}

With this small utility method you can wrap a huge block, or a single function to determine the time it takes from start, to end, and it gives you some helpers on the difference.

So what does it do? Well, first it takes the method passed as an argument:

benchmark(function () {
  console.log('Hello World');
});

and creates a start marker before it is invoked, normally it would look like this:

var start = +(new Date);
console.log('Hello World');

After it has created a start market, it invokes the method and passes along another method that should be invoked after everything is said and done.

benchmark(function (next) {
  console.log('Hello World');
  
  next();
});

Which becomes the equivelent of:

var start = +(new Date);
console.log('Hello World');
var end = +(new Date);
var difference = end - start;
difference = {
  /* Helper Details */
};

Imagine that being repeated in your code, over and over again... A huge mess right? But wait, we said something about having access to the benchmark results right? Well, to gather those, inside of the next method you pass along another method which has access to these variables. This is nested methods, asyncronous programming, or just callback nesting... Whatever stirs your cup.

benchmark(function (next) {
  console.log('Hello World');
  
  next(function (start, end, difference) {
    console.log('Processed in under: ' + difference.seconds + 's!');
  });
});

note This test will most likely, always run in under 0s, and 0ms. Add about 15 more console.log to see maybe 0.001s.

That's all there is to it, now you have a clean way of determining the difference between the start and end of your call cycle with very little clutter.

Til next time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment