Customizable, high-res replacement for console.time() for Node.js and browsers:

const ConsoleHrTime = require ( ' console-hrtime ' ) ; const timer = new ConsoleHrTime ( optionalLoggerFn ) ; timer . start ( ' foo ' ) ; timer . start ( ' bar ' ) ; setTimeout ( ( ) => { timer . endLog ( ' foo ' ) ; const ms = timer . end ( ' bar ' ) ; } , 1000 )

API

Begin measuring interval.

End the interval. Returns the elapsed time in milliseconds.

Returns the elapsed time in milliseconds.

Logs the elapsed time. The default logger outputs the elapsed time scaled to humane units (i.e. ms, sec, min, or hr depending on the size of the value) via timer.msToUnits (see below) . You can inject a custom logger function at instantiation, or you can override the instance's logger by passing a logger function to this method.

End the interval. Calls timer.log()

Utility method, returns an object: { value, units } where value is the number of milliseconds ( ms ) scaled to units at a given precision

timer.msToUnits(12.01010101, 5); // {"value":12.0101,"units":"ms"} timer.msToUnits(1234.01010101, 2); // {"value":1.23,"units":"sec"} timer.msToUnits(123456.01010101, 1); // {"value":2.1,"units":"min"} timer.msToUnits(12345678.01010101, 0); // {"value":3,"units":"hr"}

Custom logger function example

const ConsoleHrTime = require ( ' console-hrtime ' ) ; const timer = new ConsoleHrTime ( myLogger ) ; timer . start ( ' foo ' ) ; setTimeout ( ( ) => { timer . endLog ( ' foo ' ) ; } , 1000 ) function myLogger ( label , duration ) { const convert = timer . msToUnits ( duration , 1 ) ; console . log ( ` ${ label } --> ${ convert . value } ${ convert . units } ` ) ; }

Time Sources

This library automatically uses the highest resolution time source available in a given environment:

Examples

Examples can be found here