time-tree v0.3.0
time-tree
Timer utility for Node.js that gives you a contextual tree of where time is spent.
If process.hrtime()
is available, it will use it, otherwise it will use Date.now()
.
Install
To install the most recent release from npm, run:
npm install time-tree
Quick Example
var timetree = require('time-tree');
var timer = timetree('example');
timer.setContext({ actions: 2 });
return doSomething(timer, function() {
return doSomething(timer, function() {
timer.end();
console.log(timer.getResult());
});
});
function doSomething(timer, callback) {
var subTimer = timer.split('subtask');
setTimeout(function() {
subTimer.end();
return callback();
}, 100);
}
Outputs,
{ name: 'example',
duration: 205.275,
context: { actions: 2 },
timers:
[ { name: 'subtask', duration: 103.307 },
{ name: 'subtask', duration: 101.597 } ] }
See examples/
for more.
Documentation
Creates a new Timer object with the given name, and starts the timer.
Arguments
- name - Name of the timer.
Example
var timetree = require('time-tree');
var timer = timetree('myTimer');
Sets context data for the timer. This is returned with the timer data in getResult().
For example, if you're timing how long it takes to perform N actions, you may want to set N to the context.
Arguments
- context - Context data to set on the timer.
Example
timer.setContext({ actions: 2 });
Creates a new sub timer with the given name, starts the sub timer, stores it in the parent timer, and returns it.
Arguments
- name - Name of the sub timer.
Example
var subTimer = timer.split('subTimer');
Stops the timer, calculating the duration of the timer.
Note: You must stop each individual timer. Calling end()
on a parent timer will not call end()
on its sub timers.
Example
timer.end();
Returns the timer and all sub timers as a plain data object, i.e. for logging.
Example
timer.getResult();
Returns,
{ name: 'myTimer',
duration: 100,
context: { actions: 2 },
timers:
[ { name: 'subTimer', duration: 50 } ] }