Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

durations

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

durations

Duration tracking and formattng for node.js

  • 3.3.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

durations

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
  • timeAsync(function(callback)) - times a function asynchronously

Duration

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

{duration} = require 'durations'

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

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

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} = require 'durations'
watch = stopwatch()
watch.stop()  # Pauses the stopwatch. Returns the stopwatch.
watch.start() # Starts the stopwatch from where it was last stopped. Returns the stopwatch.
watch.reset() # Reset the stopwatch (duration is set back to zero). Returns the stopwatch.
duration = watch.duration() # Returns the Duration.

Methods

  • start() - start the stopwatch (no-op if already running)
  • stop() - stop the stopwatch (no-op if not running)
  • reset() - reset the stopwatch to zero elapsed time (implies stop)
  • duration() - fetch the elapsed time as a Duration
  • isRunning() - is the stopwatch running (true/false)

Timer

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

{time: timeSync, timeAsync} = require 'durations'

# Synchronous work
someFunction = ->
  count = 0
  for c in [1 .. 1000000]
    count += 1
  console.log "Count is: #{count}"

console.log "Took #{timeSync(someFunction)} to do something"

# Asynchronous work
someOtherFunction = (next) ->
  someFunction()
  next()

timeAsync someOtherFunction, (duration) ->
  console.log "Took #{duration} to do something else."

Keywords

FAQs

Package last updated on 15 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc