ProcessTimer
Implements timestamps processing in a handy simple way
/** * ProcessTimer { * VERSION : '1.0.2', * nsec : [Getter], * usec : [Getter], * msec : [Getter], * sec : [Getter] * ns : [Getter], * us : [Getter], * ms : [Getter], * s : [Getter] * } */
Designed to cover any version of NodeJs & almost any browser being in use nowadays
Utilizes process.hrtime
method if available (NodeJs v0.7.6 & above), falls back to performance.now
if available (modern browsers), in turn falls back to Date.now
(legacy browsers & NodeJs v0.7.5 & below)
Install
NodeJs
npm install process-timer
A browser
Usage
NodeJs
const ProcessTimer = const timer =
A browser
var timer =
Retrieving a timestamp
/** * A number of seconds (accurate to nanoseconds) * elapsed since a timer has been instantiated */console /** * A number of seconds (accurate to microseconds) * elapsed since a timer has been instantiated */console /** * A number of seconds (accurate to milliseconds) * elapsed since a timer has been instantiated */console /** * A number of seconds elapsed since a timer * has been instantiated */console /** * A number of nanoseconds elapsed since a timer * has been instantiated */console /** * A number of microseconds elapsed since a timer * has been instantiated */console /** * A number of milliseconds elapsed since a timer * has been instantiated */console /** * A number of seconds elapsed since a timer * has been instantiated */console
Samples
The most common use case
const ProcessTimer = /** * Launching a timer */const timer = try /** * … processing … */ /** * Retrieving a number of seconds (accurate to microseconds) to note the milestone */ console /** * Subroutine #14 */ if '-?' '-h' '--help' '--usage' /** * Launching another timer inside the subroutine */ const subroutineTimer = /** * … processing … */ /** * Retrieving a number of seconds (accurate to microseconds) to note * the completion of subroutine */ console /** * … processing … */ /** * Retrieving a number of seconds (accurate to microseconds) * to note the completion */ console catch error /** * Retrieving a number of seconds (accurate to nanoseconds) * elapsed before a failure */ console
Retrieving a number of nanoseconds / microseconds / milliseconds / seconds per se
const ProcessTimer = const timer = try /** * … processing … */ /** * Retrieving a number of microseconds to note a milestone */ console /** * … processing … */ /** * Retrieving a number of seconds along w/ a number of millisecond * to note the completion */ console catch error /** * Retrieving a number of nanoseconds elapsed before a failure */ console
The constructor accepts a text suffix to append an outcome of timer.ns
, timer.us
, timer.ms
& timer.s
(needless to say this suffix turns the type of outcome of these getters from Number
into String
)
const ProcessTimer = const timer = 's'
Hint: one can pass an empty suffix to the constructor to force the type of outcome of the above quadruplet to become String
w/ no appendix
const ProcessTimer = const timer = ''
Bugs
If you have faced some bug, please follow this link to create the issue & thanks for your time & contribution in advance!
glory to Ukraine! 🇺🇦
Juliy V. Chirkov, twitter.com/juliychirkov