Created
February 7, 2023 19:52
-
-
Save AndrwM/9c14ff6fc711cbd53b1b050162118284 to your computer and use it in GitHub Desktop.
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
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