Created
January 14, 2020 22:36
-
-
Save nojvek/58a8e109ebd5dc7b49fd0a8d751a31af to your computer and use it in GitHub Desktop.
Add marks and measures to performance timeline
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
const START = `start`; | |
const END = `end`; | |
export class PerfMeasure { | |
public measureName: string; | |
public started: boolean; | |
public ended: boolean; | |
constructor(measureName: string) { | |
this.measureName = measureName; | |
this.started = false; | |
this.ended = false; | |
} | |
start() { | |
if (!this.started) { | |
this._perfMark(START); | |
this.started = true; | |
} | |
return this; | |
} | |
end() { | |
if (this.started && !this.ended) { | |
this._perfMark(END); | |
this._perfMeasure(); | |
this.ended = true; | |
} | |
return this; | |
} | |
_perfMark(markType) { | |
if (typeof performance !== `undefined` && performance.mark) { | |
performance.mark(`${this.measureName}:${markType}`); | |
} | |
} | |
_perfMeasure() { | |
if (typeof performance !== `undefined` && performance.measure) { | |
performance.measure(this.measureName, `${this.measureName}:${START}`, `${this.measureName}:${END}`); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment