Created
January 11, 2024 19:56
-
-
Save d4nyll/cd38af12ad06a41f94f67e7a0fb19eea to your computer and use it in GitHub Desktop.
console.log() with time offset
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
function startTrackTime() { | |
console.log(` s, ms, μs, ns`); | |
console.log(`000,000,000,000: start`); | |
const start = process.hrtime.bigint(); | |
return function trackTime(message: string = '-') { | |
const end = process.hrtime.bigint(); | |
console.log( | |
`${String(end - start) | |
.padStart(12, '0') | |
.replace(/(.{3})/g, '$1,') | |
.replace(/,$/, '')}: ${message}` | |
); | |
}; | |
} | |
const trackTime = startTrackTime(); | |
trackTime('done this'); | |
trackTime('done that'); | |
// ❯ npx ts-node index.ts | |
// s, ms, μs, ns | |
// 000,000,000,000: start | |
// 000,000,020,000: done this | |
// 000,000,080,000: done that |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment