Skip to content

Instantly share code, notes, and snippets.

@nojvek
Created January 14, 2020 22:36
Show Gist options
  • Save nojvek/58a8e109ebd5dc7b49fd0a8d751a31af to your computer and use it in GitHub Desktop.
Save nojvek/58a8e109ebd5dc7b49fd0a8d751a31af to your computer and use it in GitHub Desktop.
Add marks and measures to performance timeline
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