@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
62 lines (61 loc) • 1.39 kB
JavaScript
/**
* using _ = blockTimer()
* // will log "took 1.234 sec" on dispose
*
* using _ = blockTimer('named')
* // will log "named took 1.234 sec" on dispose
*
* @experimental
*/
export function _blockTimer(name) {
const started = Date.now();
return {
[Symbol.dispose]() {
console.log(`${name ? name + ' ' : ''}took ${_since(started)}`);
},
};
}
/**
* Returns time passed since `from` until `until` (default to Date.now())
*/
export function _since(from, until = Date.now()) {
return _ms(until - from);
}
/**
* Returns, e.g:
* 125 ms
* 1.125 sec
* 11 sec
* 1m12s
* 59m2s
* 1h3m12s
*/
export function _ms(millis) {
// <1 sec
if (millis < 1000)
return `${Math.round(millis)} ms`;
// < 10 sec
if (millis < 10_000) {
const s = millis / 1000;
return `${s.toFixed(2)} sec`;
}
const sec = Math.floor(millis / 1000) % 60;
const min = Math.floor(millis / (60 * 1000)) % 60;
const hrs = Math.floor(millis / (3600 * 1000));
// <1 hr
if (hrs === 0) {
// <1 min
if (min === 0)
return `${sec} sec`;
return `${min}m${sec}s`;
}
if (hrs < 24) {
return `${hrs}h${min}m`;
}
if (hrs < 48) {
return `${Math.round(hrs + min / 60)}h`;
}
// >= 48 hours
const days = Math.floor(hrs / 24);
return `${days} days`;
}