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

durations

v3.4.2

Published

Duration tracking and formattng for node.js

Downloads

1,130,486

Readme

durations

Build Status NPM version

Compatibilty

Both Node.js and browsers are supported by durations. When using Node.js, the nanosecond-granulatiry process.hrtime() function is used. The best substitution is selected when in the browser such that consistency is maintained even if time granularity cannot be.

Installation

npm install --save durations

Methods

The following functions are exported:

  • duration(nanoseconds) - constructs a new Duration
  • nanos(nanoseconds) - constructs a new Duration
  • micros(microseconds) - constructs a new Duration
  • millis(milliseconds) - constructs a new Duration
  • seconds(seconds) - constructs a new Duration
  • stopwatch() - constructs a new Stopwatch (stopped)
  • time(function) - times a function synchronously
  • timeAsync(function(callback)) - times a function asynchronously
  • timePromised(function()) - times a promise-returning function

Duration

Represents a duration with nanosecond granularity, and provides methods for converting to other granularities, and formatting the duration.

Methods

  • format() - human readable string representing the duration
  • nanos() - duration as nanoseconds
  • micros() - duration as microseconds
  • millis() - duration as milliseconds
  • seconds() - duration as seconds
  • minutes() - duration as minutes
  • hours() - duration as hours
  • days() - duration as days
const {duration} = require('durations')

const nanoseconds = 987654321
console.log("Duration is", duration(nanoseconds).format())

// Or, since toString() is an alias to format()
console.log(`Duration is ${duration(nanoseconds)}`)

Stopwatch

A nanosecond granularity (on Node.js) stopwatch with chainable control methods, and built-in formatting.

Stopwatch Methods

  • start() - start and return the stopwatch (no-op if already running)
  • stop() - stop and return the stopwatch (no-op if not running)
  • reset() - reset to zero elapsed time and return the stopwatch (implies stop)
  • duration() - fetch the elapsed time as a Duration
  • isRunning() - is the stopwatch running (true/false)
const {stopwatch} = require('durations')
const watch = stopwatch()

// Pauses the stopwatch. Returns the stopwatch.
watch.stop()

// Starts the stopwatch from where it was last stopped. Returns the stopwatch.
watch.start()

// Reset the stopwatch (duration is set back to zero). Returns the stopwatch.
watch.reset()

console.log(`${watch.duration().seconds()} seconds have elapsed`)
// OR
console.log(`${watch} have elapsed`)

Timer

Times the execution of a function, and returns the duration.

const {time: timeSync, timeAsync} = require('durations')

// Synchronous work
const someFunction = () => {
  let count = 0

  while (count < 1000000) {
    count++
  }

  console.log(`Count is: ${count}`)
}

console.log(`Took ${timeSync(someFunction)} to do something`)

// Asynchronous work
const someOtherFunction = next => {
  someFunction()
  next()
}

timeAsync(someOtherFunction, duration => {
  console.log(`Took ${duration} to do something else.`)
})

// Promised work
const somePromisedOp = () => {
  return new Promise((resolve) => {
    someFunction()
    resolve()
  })
}

timePromised(somePromisedOp)
.then(duration => {
  console.log(`Took ${duration} to keep promise.`)
})