Skip to content

Instantly share code, notes, and snippets.

@AndrwM
Created February 7, 2023 19:52
Show Gist options
  • Save AndrwM/9c14ff6fc711cbd53b1b050162118284 to your computer and use it in GitHub Desktop.
Save AndrwM/9c14ff6fc711cbd53b1b050162118284 to your computer and use it in GitHub Desktop.
export class Timer {
static FORMAT = {
NANOSECOND: 'ns',
MILLISECOND: 'ms',
} as const;
private start = this.now;
private get now() {
return process.hrtime.bigint();
}
public duration(
format: (typeof Timer.FORMAT)[keyof typeof Timer.FORMAT],
): number {
const elapsedNanoseconds = Number(this.start - this.now);
switch (format) {
case Timer.FORMAT.NANOSECOND:
return elapsedNanoseconds;
case Timer.FORMAT.MILLISECOND:
return Math.round(elapsedNanoseconds / 1000000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment