Skip to content

Instantly share code, notes, and snippets.

@cevaris
Last active June 3, 2020 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cevaris/047a6c5c6ea035f7f3f403c23635386b to your computer and use it in GitHub Desktop.
Save cevaris/047a6c5c6ea035f7f3f403c23635386b to your computer and use it in GitHub Desktop.
Simple stopwatch that measures overall time.
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