process-timer
v1.0.2
Published
High-resolution timer class for NodeJs & browsers
Downloads
5
Maintainers
Readme
ProcessTimer
High-resolution timer class
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
Obtain from
<script src="https://cdn.jsdelivr.net/npm/process-timer@1/process-timer.min.js" async></script>
Usage
NodeJs
const ProcessTimer = require('process-timer')
const timer = new ProcessTimer()
A browser
var timer = new ProcessTimer()
Retrieving a timestamp
/**
* A number of seconds (accurate to nanoseconds)
* elapsed since a timer has been instantiated
*/
console.log(timer.ns)
/**
* A number of seconds (accurate to microseconds)
* elapsed since a timer has been instantiated
*/
console.log(timer.us)
/**
* A number of seconds (accurate to milliseconds)
* elapsed since a timer has been instantiated
*/
console.log(timer.ms)
/**
* A number of seconds elapsed since a timer
* has been instantiated
*/
console.log(timer.s)
/**
* A number of nanoseconds elapsed since a timer
* has been instantiated
*/
console.log(timer.nsec)
/**
* A number of microseconds elapsed since a timer
* has been instantiated
*/
console.log(timer.usec)
/**
* A number of milliseconds elapsed since a timer
* has been instantiated
*/
console.log(timer.msec)
/**
* A number of seconds elapsed since a timer
* has been instantiated
*/
console.log(timer.sec)
Samples
The most common use case
const ProcessTimer = require('process-timer')
/**
* Launching a timer
*/
const timer = new ProcessTimer()
try {
/**
* … processing …
*/
/**
* Retrieving a number of seconds (accurate to microseconds) to note the milestone
*/
console.log('Code block of Subroutine #14 has been reached on %s sec', timer.us)
/**
* Subroutine #14
*/
if (['-?', '-h', '--help', '--usage'].includes(process.argv[2])) {
/**
* Launching another timer inside the subroutine
*/
const subroutineTimer = new ProcessTimer()
/**
* … processing …
*/
/**
* Retrieving a number of seconds (accurate to microseconds) to note
* the completion of subroutine
*/
console.log('Subroutine #14 time: %s sec', subroutineTimer.us)
}
/**
* … processing …
*/
/**
* Retrieving a number of seconds (accurate to microseconds)
* to note the completion
*/
console.log('Total time: %s sec', timer.us)
} catch (error) {
/**
* Retrieving a number of seconds (accurate to nanoseconds)
* elapsed before a failure
*/
console.error('Crashed on %s sec\n%s', timer.ns, error.stack)
}
Retrieving a number of nanoseconds / microseconds / milliseconds / seconds per se
const ProcessTimer = require('process-timer')
const timer = new ProcessTimer()
try {
/**
* … processing …
*/
/**
* Retrieving a number of microseconds to note a milestone
*/
console.log('Got here after %s μs', timer.usec)
/**
* … processing …
*/
/**
* Retrieving a number of seconds along w/ a number of millisecond
* to note the completion
*/
console.log('Total time: %s sec (%s ms)', timer.sec, timer.msec)
} catch (error) {
/**
* Retrieving a number of nanoseconds elapsed before a failure
*/
console.error('Crashed on %s ns\n%s', timer.nsec, error.stack)
}
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 = require('process-timer')
const timer = new ProcessTimer('s')
setTimeout(() => {
const milestone = timer.us
/**
* Expected output:
* 'string'
* '8.008321s'
*/
console.log(typeof milestone)
console.log(milestone)
}, 8000)
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 = require('process-timer')
const timer = new ProcessTimer('')
setTimeout(() => {
const milestone = timer.us
/**
* Expected output:
* 'string'
* '8.008321'
*/
console.log(typeof milestone)
console.log(milestone)
}, 8000)
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