npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

process-timer

v1.0.2

Published

High-resolution timer class for NodeJs & browsers

Downloads

5

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