Skip to content

Commit

Permalink
refactor(duration): add current time calculation as a separate var
Browse files Browse the repository at this point in the history
Use decimalPlaces property in timeUnits array for better formatting
  • Loading branch information
PunGrumpy committed May 30, 2024
1 parent b9133ee commit 5c4e9b1
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/utils/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import chalk from 'chalk'
* @returns {string} A formatted duration string including the time unit.
*/
function durationString(beforeTime: bigint): string {
const nanoseconds = Number(process.hrtime.bigint() - beforeTime)
const currentTime = process.hrtime.bigint()
const nanoseconds = Number(currentTime - beforeTime)

const timeUnits = [
{ unit: 's', threshold: 1e9 },
{ unit: 'ms', threshold: 1e6 },
{ unit: 'µs', threshold: 1e3 }
{ unit: 's', threshold: 1e9, decimalPlaces: 2 },
{ unit: 'ms', threshold: 1e6, decimalPlaces: 0 },
{ unit: 'µs', threshold: 1e3, decimalPlaces: 0 }
]

for (const { unit, threshold } of timeUnits) {
for (const { unit, threshold, decimalPlaces } of timeUnits) {
if (nanoseconds >= threshold) {
const value = (nanoseconds / threshold).toFixed(threshold === 1e9 ? 2 : 0)
const value = (nanoseconds / threshold).toFixed(decimalPlaces)
return formatTime(value, unit)
}
}
Expand Down

0 comments on commit 5c4e9b1

Please sign in to comment.