Timing Grunt Task Duration

Timing is everything

I recently blogged about the Grunt JavaScript task runner, a great way to automate repetitive tasks.

Some plugins I use (such as the jasmine plugin) output how long they take to run (to the console). This is a pretty neat feature, but very few plugins do it.

Obviously you don’t want to fork each plugin (there are 100s) and implement a timer in each, so I had another idea.

grunt-timer

I threw grunt-timer.js together in a couple of hours. It uses the documented hooker.(un)hook to (as closely as possible) determine the elapsed execution time for each task as they are run by Grunt.

Shouldn’t this be part of Grunt?

I think so, possibly as an option that can be configured. It would have been a lot easier to implement too, it’s a little hacky the way I’ve done it using hooks and the time isn’t 100% accurate.

Hopefully people will find this useful and the equivalent will find its way into the core.

How do I use it?

1
npm install grunt-timer ## --save

Add the grunt-timer reference to the very top of your Gruntfile.js:

1
var timer = require("grunt-timer");

Add the timer init call at the top of the module.exports function of your Gruntfile.js::

1
2
module.exports = function (grunt) {
timer.init(grunt);

Example output

1
2
3
Running "run:500" (run) task
500ms task ran
Task 'run:500' took 502ms

Issues

I haven’t spent too much time on this so far, and there are a couple of issues that exist:

  • The last task duration is output after the “Done, without errors”.
  • Tasks that wrap multi-tasks are currently output (shouldn’t be, or if they are, should be a summary of all the multitasks).

Feel free to fork and pull request on the git repo.