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 = ; var timer = ;timer;return ; { var subTimer = timer; ;}
Outputs,
name: 'example' duration: 205275 context: actions: 2 timers: name: 'subtask' duration: 103307 name: 'subtask' duration: 101597
See examples/
for more.
Documentation
- Constructor
- getName
- getDuration
- getContext
- setContext
- split
- end
- wrap
- getSubTimer
- getSubTimers
- getResult
Creates a new Timer object with the given name, and starts the timer.
Arguments
- name - Name of the timer.
- context - Optional, see setContext().
Example
var timetree = ;var timer = ;
Gets the timer name.
### getDuraton()Gets the timer duration.
### getContext()Gets the context data for the timer.
### setContext(context)Sets the 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;
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.
- context - Optional context metadta for the sub timer, see setContext().
Example
var subTimer = timer;
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;
Wraps a callback to include a call to end().
Arguments
- callback - Function to wrap
Example
// Instead of thisfs // You can do thisfs;
Returns the first sub timer object that matches the given name.
Arguments
- name - Name of timer to search for.
- recursive - Whether to search recursively. Optional. Defaults to FALSE.
Example
timer;
Returns the sub timer objects that match the given name.
Arguments
- name - Name of timer to search for.
- recursive - Whether to search recursively. Optional. Defaults to FALSE.
Example
timer;
Returns the timer and all sub timers as a plain data object, i.e. for logging.
Example
timer;
Returns,
name: 'myTimer' duration: 100 context: actions: 2 timers: name: 'subTimer' duration: 50