Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/juliyvchirkov/process-timer

High-resolution timer class for NodeJs & browsers
https://github.com/juliyvchirkov/process-timer

browser class development epoch es5 high-resolution hrtime javascript microseconds milliseconds nanoseconds nodejs performance process profiling time timer timestamp tool utility

Last synced: 14 days ago
JSON representation

High-resolution timer class for NodeJs & browsers

Awesome Lists containing this project

README

        

# ProcessTimer
**High-resolution timer class**

Implements timestamps processing in a handy simple way

```javascript
/**
* 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**

```bash
npm install process-timer
```

**A browser**

Obtain from

```html

```

## Usage

**NodeJs**

```javascript
const ProcessTimer = require('process-timer')
const timer = new ProcessTimer()
```

**A browser**

```javascript
var timer = new ProcessTimer()
```

Retrieving a timestamp

```javascript
/**
* 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

```javascript
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

```javascript
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`)

```javascript
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

```javascript
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](https://github.com/juliyvchirkov/process-timer/issues) & thanks for your time & contribution in advance!

**glory to Ukraine!** 🇺🇦

Juliy V. Chirkov, [twitter.com/juliychirkov](https://twitter.com/juliychirkov)