1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/timer.js

29 lines
727 B
JavaScript
Raw Normal View History

'use strict';
const NS_TO_S = 1e9;
// seconds takes a tuple of [seconds, nanoseconds]
// and returns the time in seconds
const seconds = diff => diff[0] + diff[1] / NS_TO_S;
module.exports = {
// new returns a timer function. Call it to measure the time since the call to new().
// the timer function returns the duration in seconds
//
// usage:
//
// t = timer.new()
// setTimeout(() => {
// diff = t()
// console.log(diff) // 0.500003192s
// }, 500)
//
new: () => {
const now = process.hrtime();
// the timer function returns the time in seconds
// since new() was called
return () => seconds(process.hrtime(now));
},
seconds,
};