# durations

> Duration tracking and formattng for node.js

Latest version **3.4.2** (published 2019-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install durations
pnpm add durations
yarn add durations
bun add durations
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.4.2 |
| Published | 2019-11-26 |
| First published | 2015-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 0 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Joel Edwards |
| Maintainers | buzuli, joeledwards |
| Keywords | Stopwatch, Monitoring, Performance, Duration |

## Links

- npm: https://www.npmjs.com/package/durations
- Repository: https://github.com/joeledwards/node-durations
- Homepage: https://github.com/joeledwards/node-durations#readme
- Issues: https://github.com/joeledwards/node-durations/issues
- npm.io page: https://npm.io/package/durations

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.4.2 (latest) — 2019-11-26
- 3.4.1 — 2018-01-20
- 3.4.0 — 2018-01-20
- 3.3.5 — 2018-01-15
- 3.3.4 — 2018-01-15
- 3.3.3 — 2018-01-15
- 3.3.2 — 2018-01-15
- 3.3.1 — 2018-01-15
- 3.3.0 — 2018-01-05
- 3.2.1 — 2017-11-21
- 3.2.0 — 2017-11-21
- 3.1.0 — 2017-11-21
- 3.0.0 — 2016-08-20
- 2.2.0 — 2016-05-17
- 2.1.0 — 2016-04-09
- … 5 more at https://npm.io/package/durations/versions

## README

# durations

[![Build Status][travis-image]][travis-url]
[![NPM version][npm-image]][npm-url]

## 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

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

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

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

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

[travis-url]: https://travis-ci.org/joeledwards/node-durations
[travis-image]: https://img.shields.io/travis/joeledwards/node-durations/master.svg
[npm-url]: https://www.npmjs.com/package/durations
[npm-image]: https://img.shields.io/npm/v/durations.svg

---
_Source: https://npm.io/package/durations · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
