-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasure.ts
148 lines (136 loc) · 6.39 KB
/
measure.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// References:
// https://w3c.github.io/user-timing/#dom-performance-measure
// https://deno.land/x/[email protected]/ext/web/15_performance.js
// 4.1. Convert a mark to a timestamp
function convertMarkToTimestamp(mark: string | number) {
if (typeof mark === "string") {
const entries = performance
.getEntriesByType("mark")
.filter((m) => m.name === mark);
const entry = entries[entries.length - 1];
if (!entry) {
throw new DOMException(`Cannot find mark: "${mark}".`, "SyntaxError");
}
return entry.startTime;
}
if (mark < 0) {
throw new TypeError("Mark cannot be negative.");
}
return mark;
}
// 3.1.3 measure() method
export function performanceMeasurePolyfill(
measureName: string,
startOrMeasureOptions?: PerformanceMeasureOptions | string,
endMark?: string,
): PerformanceMeasure | never {
let startTime: number | undefined = undefined;
let endTime: number | undefined = undefined;
const isOptions = (v: unknown): v is PerformanceMeasureOptions =>
typeof v === "object" && v !== null && !Array.isArray(v);
// 1. If startOrMeasureOptions is a PerformanceMeasureOptions object and at least one of start, end, duration, and detail exist, run the following checks:
if (isOptions(startOrMeasureOptions)) {
// 1.1. If endMark is given, throw a TypeError.
if (endMark) {
throw new TypeError(
"If startOrMeasureOptions is an object, endMark cannot be provided.",
);
}
if (
// 1.2. If startOrMeasureOptions's start and end members are both omitted, throw a TypeError.
(startOrMeasureOptions.start === undefined &&
startOrMeasureOptions.end === undefined) ||
// 1.3. If startOrMeasureOptions's start, duration, and end members all exist, throw a TypeError.
(startOrMeasureOptions.start !== undefined &&
startOrMeasureOptions.end !== undefined &&
startOrMeasureOptions.duration !== undefined)
) {
throw new TypeError("Invalid startOrMeasureOptions.");
}
}
// 2. Compute end time as follows:
// 2.1. If endMark is given, let end time be the value returned by running the convert a mark to a timestamp algorithm passing in endMark.
if (endMark !== undefined && typeof startOrMeasureOptions === "string") {
endTime = convertMarkToTimestamp(endMark);
}
// 2.2. Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object, and if its end member exists, let end time be the value returned by running the convert a mark to a timestamp algorithm passing in startOrMeasureOptions's end.
if (isOptions(startOrMeasureOptions)) {
if (startOrMeasureOptions.end !== undefined) {
endTime = convertMarkToTimestamp(startOrMeasureOptions.end);
} // 2.3. Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object, and if its start and duration members both exist:
else if (
startOrMeasureOptions.start !== undefined &&
startOrMeasureOptions.duration !== undefined
) {
// 2.3.1. Let start be the value returned by running the convert a mark to a timestamp algorithm passing in start.
const start = convertMarkToTimestamp(startOrMeasureOptions.start);
// 2.3.2. Let duration be the value returned by running the convert a mark to a timestamp algorithm passing in duration.
const duration = convertMarkToTimestamp(startOrMeasureOptions.duration);
// 2.3.3. Let end time be start plus duration.
endTime = start + duration;
} // 2.4. Otherwise, let end time be the value that would be returned by the Performance object's now() method.
else {
endTime = performance.now();
}
} // 2.4. Otherwise, let end time be the value that would be returned by the Performance object's now() method.
else {
endTime = performance.now();
}
// 3. Compute start time as follows:
// 3.1. If startOrMeasureOptions is a PerformanceMeasureOptions object, and if its start member exists, let start time be the value returned by running the convert a mark to a timestamp algorithm passing in startOrMeasureOptions's start.
if (isOptions(startOrMeasureOptions)) {
if (startOrMeasureOptions.start !== undefined) {
startTime = convertMarkToTimestamp(startOrMeasureOptions.start);
} // 3.2. Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object, and if its duration and end members both exist:
else if (
startOrMeasureOptions.duration !== undefined &&
startOrMeasureOptions.end !== undefined
) {
// 3.2.1. Let duration be the value returned by running the convert a mark to a timestamp algorithm passing in duration.
const duration = convertMarkToTimestamp(startOrMeasureOptions.duration);
// 3.2.2. Let end be the value returned by running the convert a mark to a timestamp algorithm passing in end.
const end = convertMarkToTimestamp(startOrMeasureOptions.end);
// 3.2.3. Let start time be end minus duration.
startTime = end - duration;
}
} // 3.3. Otherwise, if startOrMeasureOptions is a DOMString, let start time be the value returned by running the convert a mark to a timestamp algorithm passing in startOrMeasureOptions.
else if (typeof startOrMeasureOptions === "string") {
startTime = convertMarkToTimestamp(startOrMeasureOptions);
} // 3.4. Otherwise, let start time be 0.
else {
startTime = 0;
}
if (startTime === undefined || endTime === undefined) {
throw new TypeError("Invalid startTime or endTime");
}
// 4. Create a new PerformanceMeasure object (entry) with this's relevant realm.
const entry: PerformanceMeasure = {
// 5. Set entry's name attribute to measureName.
name: measureName,
// 6. Set entry's entryType attribute to DOMString "measure".
entryType: "measure",
// 7. Set entry's startTime attribute to start time.
startTime,
// 8. Set entry's duration attribute to the duration from start time to end time. The resulting duration value MAY be negative.
duration: endTime - startTime,
// 9. Set entry's detail attribute as follows:
detail: isOptions(startOrMeasureOptions)
? startOrMeasureOptions.detail ?? null
: null,
toJSON() {
return {
name: this.name,
entryType: this.entryType,
startTime: this.startTime,
duration: this.duration,
detail: this.detail,
};
},
};
// 10. Queue entry.
// TODO
// 11. Add entry to the performance entry buffer.
// TODO
// 12. Return entry.
return entry;
}