atlas-hrtime
v1.0.1
Published
Returns arbitrary time diffs in nanoseconds, falling back to performance.now or Date.now if process.hrtime is not available.
Downloads
10
Maintainers
Readme
atlas-hrtime
Returns arbitrary time diffs in nanoseconds, falling back to performance.now or Date.now if process.hrtime is not available.
install
npm install --save atlas-hrtime
why
process.hrtime
, performance.now
and Date.now
are not consistent. This module exports a clock which uses whichever one is available (in the precedence defined here), where all units are normalized to nanoseconds. This module is meant for time diffs, not asking for the current datetime. If you need the current datetime, use javascript's built-in Date
function or a wrapper library like moment
.
examples
recording a time
To record the time in nano-seconds, just call the clock:
const clock = require("atlas-hrtime")();
console.log(`current time measured from an arbitrary time in the past is ${clock()}ns`)
calculating a time diff
Let's say you need to test how long your arraySort
algorithm takes. All you need to do is call the clock before running your sort, then call it after running your sort with the previous time as the argument:
...
const initialTime = clock()
arraySort(myLongArray)
const deltaTime = clock(initialTime)
console.log(`arraySort took ${deltaTime}ns to sort myLongArray`)
calculating multiple time diffs
Alternatively, you can record down times at certain points and then manually log the diffs:
...
const t0 = clock()
quickSort(getLongArray())
const t1 = clock()
heapSort(getLongArray())
const t2 = clock()
insertionSort(getLongArray())
const t3 = clock();
console.log(`quickSort took ${t1-t0}ns`)
console.log(`heapSort took ${t2-t1}ns`)
console.log(`insertionSort took ${t3-t2}ns`)
caveats
calling the module
Make sure you call the module before using it (e.g. require("atlas-hrtime")()
). The return value of the exported function is the clock. This was done to keep the module easily testable.
units
All units are in nanoseconds, plain and simple -- this keeps the module minimal. You can convert to other units manually.