Skip to content

Instantly share code, notes, and snippets.

@d4nyll
Created January 11, 2024 19:56
Show Gist options
  • Save d4nyll/cd38af12ad06a41f94f67e7a0fb19eea to your computer and use it in GitHub Desktop.
Save d4nyll/cd38af12ad06a41f94f67e7a0fb19eea to your computer and use it in GitHub Desktop.
console.log() with time offset
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