-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
31 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Local Variables | ||
const timeUnits = ['ns', 'μs', 'ms', 's'] as const; | ||
|
||
// Local Functions | ||
function getElapsedTimeFormatted(startTime: number) { | ||
const endTime = Bun.nanoseconds(); | ||
let elapsedTime = endTime - startTime; | ||
let timeIndex = 0; | ||
while (elapsedTime > 1) { | ||
if (elapsedTime <= 1_000) { | ||
break; | ||
} | ||
elapsedTime /= 1_000; | ||
timeIndex += 1; | ||
} | ||
const elapsedTimeLocalized = elapsedTime.toLocaleString(undefined, { | ||
maximumFractionDigits: 2, | ||
minimumFractionDigits: 2, | ||
}); | ||
const units = timeUnits[timeIndex]; | ||
return `${elapsedTimeLocalized}${units}`; | ||
} | ||
|
||
// Module Exports | ||
export { getElapsedTimeFormatted }; |