Last active
June 3, 2020 21:22
-
-
Save cevaris/047a6c5c6ea035f7f3f403c23635386b to your computer and use it in GitHub Desktop.
Simple stopwatch that measures overall time.
This file contains hidden or 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
interface StopwatchResults { | |
startDate: Date | |
endDate: Date | |
latencyMs: number | |
} | |
class Stopwatch { | |
private startDate?: Date | |
private endDate?: Date | |
start(): void { | |
this.startDate = new Date(); | |
} | |
stop(): void { | |
this.endDate = new Date(); | |
} | |
results(): StopwatchResults { | |
if (this.startDate && this.endDate) { | |
return { | |
latencyMs: +this.endDate - +this.startDate, | |
startDate: this.startDate, | |
endDate: this.endDate | |
} | |
} else { | |
throw Error('Stopwatch was never started/started'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment