Skip to content

Commit

Permalink
Merge b1eda08 into 7e224b9
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlup committed Nov 9, 2020
2 parents 7e224b9 + b1eda08 commit 9a05382
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [Usage](#usage)
- [API](#api)
* [hrtime2ns(time)](#hrtime2nstime)
* [hrtime2us(time)](#hrtime2ustime)
* [hrtime2ms(time)](#hrtime2mstime)
* [hrtime2s(time)](#hrtime2stime)

Expand All @@ -30,19 +31,22 @@ $ npm i @dnlup/hrtime-utils
```js
const {
hrtime2ns,
hrtime2us,
hrtime2ms,
hrtime2
} = require('@dnlup/hrtime-utils')
const time = process.hrtime()
hrtime2ns(time) // time in nanoseconds
hrtime2us(time) // time in microseconds
hrtime2ms(time) // time in milliseconds
hrtime2s(time) // time in seconds
const delta = process.hrtime(time)
hrtime2ns(delta) // delta in nanoseconds
hrtime2us(delta) // delta in microseconds
hrtime2ms(delta) // delta in milliseconds
hrtime2s(delta) // delta in seconds
```
Expand All @@ -57,6 +61,14 @@ hrtime2s(delta) // delta in seconds

This function converts `time` to nanoseconds.

### hrtime2us(time)

* `time` `<integer[]`> The return value of a [`process.hrtime()`](https://nodejs.org/docs/latest-v12.x/api/process.html#process_process_hrtime_time) call

* Returns `<number>`

This function converts `time` to microseconds.

### hrtime2ms(time)

* `time` `<integer[]`> The return value of a [`process.hrtime()`](https://nodejs.org/docs/latest-v12.x/api/process.html#process_process_hrtime_time) call
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ exports.hrtime2ns = function (time) {
return time[0] * 1e9 + time[1]
}

exports.hrtime2us = function (time) {
return time[0] * 1e6 + time[1] / 1e3
}

exports.hrtime2ms = function (time) {
return time[0] * 1e3 + time[1] / 1e6
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "standard | snazzy",
"test": "npm run lint && tap test.js --100",
"test": "npm run lint && tap test.js",
"test:ci": "npm run test -- --cov --coverage-report=lcovonly",
"doc": "markdown-toc -i README.md",
"prerelease": "npm cit",
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const tap = require('tap')
const sleep = require('atomic-sleep')
const {
hrtime2ns,
hrtime2us,
hrtime2ms,
hrtime2s
} = require('./')
Expand All @@ -17,6 +18,16 @@ tap.test('hrtime2ns', t => {
t.end()
})

tap.test('hrtime2us', t => {
const start = process.hrtime()
sleep(205)
const end = process.hrtime(start)
const delta = hrtime2us(end)
console.log(delta)
t.ok(delta >= 2e5, `value ${delta}`)
t.end()
})

tap.test('hrtime2ms', t => {
const start = process.hrtime()
sleep(205)
Expand Down

0 comments on commit 9a05382

Please sign in to comment.