1.0.6 • Published 5 years ago

hrtime-now v1.0.6

Weekly downloads
47
License
MIT
Repository
github
Last release
5 years ago

hrtime-now

Friendly API for process.hrtime with some high level - date-pretty format utilities. Ideal to measure how long it take to run a function /fragment of code.

Install

npm install --save hrtime-now

Usage

Basic: Using now() :

import {now} from 'hrtime-now'
const t0 = now()
// execute synchronous some task to measure time
console.log(`the task took ${(now()-t0)/1000000} milliseconds`)

Important: the number returned by now doesn't mean anything special, i.e, is not performance.now() !

High level: pretty times with timeFrom()

Pretty print the difference between given time (previouslytaken with now() and current time). Example:

var t0 = now()
something()
log(`something took ${timeFrom(t0)}`)

Could print : "somehing took 1m 29s"

High level: single statement with fromNow()

Suppose you have a (synchronous) statement like the following

const result = compileSass('./src/**/*.scss')

You want to measure how long it takes:

const result = fromNow(
  ()=>compileSass('./src/**/*.scss'), 
  t=>console.log(`sass compilation took ${t}`)
)

or create a log function factory and don't worry about msgs:

const logTime = (fn) => fromNow(fn, (t, hint) => console.log(`Function ${hint} took ${t}`))
// use logTime() instead of fromNow from now on without worrying to pass any msg 
const result = logTime(() => compileSass('./src/**/*.scss'))
// will log something like "Function ()=>compileSass took 1s"

Using time() :

import {time} from 'hrtime-now'
const t1 = time()
// execute some task to measure time
const t2 = time(t1)
console.log(`Task took ${t2/1000000}` milliseconds`)

Note: If you are not using ecma6 modules, you can }require()now()` like this:

const now = require('hrtime-now').now

Working Example:

API documentation

API

TODO

  • asynchronous