hitime

0.3.0 • Public • Published

hitime

Build Test Coverage Code Climate Downloads Version Dependency Status

⏰ Hi-res timer for Node, wrapped in some pretty helpers.

Install

npm install -S hitime

Use

To get a simple high resolution timestamp:

var hitime = require('hitime');
 
var timestamp = hitime();

This will return a decimal number, in milliseconds (so you can still do mental math), accurate to the nanosecond, using Node's native process.hrtime. This is a relative time, measured from the time that the module was loaded.

You can compare two timestamps with regular math, because they are just numbers:

var a = hitime();
var b = hitime();
 
var duration = b - a;

Using named timers

Similarly to Chrome's console.time, this module gives you named timers, so you can keep easier track of various tasks.

var timer = hitime.Timer();
 
timer.start('async work');
 
doAsyncWork(function(err) {
    // with high-resolution time, you want
    // to make sure you call this as soon as possible
    timer.end('async work');
 
    // check for an error now... if statements do cost
    // time, you know
 
    timer.start('heavy work');
 
    ...
 
    timer.end('heavy work');
 
    // at some point in the future
    var report = timer.report();
 
    console.log('async work in %s ms', report['async work'].duration);
    console.log('heavy work in %s ms', report['heavy work'].duration);
});

Any timers that have ben registered will appear in the report, containing the following values:

  • start: the relative start time of the timer
  • end: the relative end time of the timer
  • duration: the total time for the timer

Readme

Keywords

none

Package Sidebar

Install

npm i hitime

Weekly Downloads

68

Version

0.3.0

License

ISC

Last publish

Collaborators

  • kirilv