-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathtime.ts
More file actions
40 lines (34 loc) · 1.18 KB
/
time.ts
File metadata and controls
40 lines (34 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Tracks the time a method takes to complete using the [performance API](https://developer.mozilla.org/en-US/docs/Web/API/Performance)
*/
export function time(tag?: string) {
return function(_: unknown, key: string, descriptor: PropertyDescriptor) {
const { value: f } = descriptor ?? {};
if (!(typeof f === 'function')) {
throw new Error('@time() may only decorate class methods');
}
descriptor.value = function(...args: any[]) {
const TAG = tag ?? `${this.constructor.name}-${key}`;
const START_TAG = `start-${TAG}`;
const END_TAG = `end-${TAG}`;
if (window.PfeConfig.trackPerformance) {
window.performance.mark(START_TAG);
}
const x = f.call(this, ...args);
const ret = () => {
if (window.PfeConfig.trackPerformance) {
window.performance.mark(END_TAG);
window.performance.measure(TAG, START_TAG, END_TAG);
// eslint-disable-next-line no-console
console.log(Array.from(window.performance.getEntriesByName(TAG)).pop());
}
return x;
};
if (x instanceof Promise) {
return x.then(ret);
} else {
return ret();
}
};
};
}