Converts a time in nanoseconds to a pretty string in the most convenient units.
npm install --save atlas-pretty-hrtime
Printing times is annoying, so this is me thinking about it once and hopefully never again. If you are doing performance testing, you will encounter time diffs between different points in your code. With this module, you don't need to worry about how to print those times in various units with varying precision -- it's done automatically for you.
The exported function takes a
time in nanoseconds, an optional precision integer and returns a string. The default precision is
3 decimal places, and the maximum unit is hours.
const pretty = ;// all times in nanosecondsconst times =// output units:0 // ns123 // ns1234 // us12345 // us123456 // us1234567 // ms12345678 // ms123456789 // ms1234567890 // sec12345678901 // sec123456789012 // min1234567890123 // min12345678901234 // hrtimes;// 0.0ns// 123.0ns// 1.2us// 12.3us// 123.5us// 1.2ms// 12.3ms// 123.5ms// 1.2sec// 12.3sec// 2.1min// 20.6min// 3.4hr
Only accepts times in nanoseconds, because supporting multiple input units is outside the scope of this package. This package is primarily for printing time diffs, which should not be in milliseconds or seconds if you're using high resolution time. See atlas-hrtime.
fractional minutes and hours
For simplicity, this module does not decompose fractional minutes into "minutes and seconds" (and similarly with hours). This doesn't bother me much, so I'm unlikely to implement something like:
console// 2min 3sec
specifying return units
At the moment, this is not possible, but I may implement it in the future if I need finer-grained control on the output units: