1
1
'use strict' ;
2
2
3
3
const {
4
- ObjectDefineProperty,
5
4
ObjectDefineProperties,
6
- ObjectSetPrototypeOf,
5
+ ReflectConstruct,
6
+ SymbolToStringTag,
7
7
} = primordials ;
8
8
9
9
const {
@@ -17,9 +17,13 @@ const {
17
17
EventTarget,
18
18
Event,
19
19
kTrustEvent,
20
+ initEventTarget,
20
21
} = require ( 'internal/event_target' ) ;
21
22
22
- const { now } = require ( 'internal/perf/utils' ) ;
23
+ const {
24
+ now,
25
+ kPerformanceBrand,
26
+ } = require ( 'internal/perf/utils' ) ;
23
27
24
28
const { markResourceTiming } = require ( 'internal/perf/resource_timing' ) ;
25
29
@@ -38,8 +42,9 @@ const {
38
42
const { eventLoopUtilization } = require ( 'internal/perf/event_loop_utilization' ) ;
39
43
const nodeTiming = require ( 'internal/perf/nodetiming' ) ;
40
44
const timerify = require ( 'internal/perf/timerify' ) ;
41
- const { customInspectSymbol : kInspect } = require ( 'internal/util' ) ;
45
+ const { customInspectSymbol : kInspect , kEnumerableProperty , kEmptyObject } = require ( 'internal/util' ) ;
42
46
const { inspect } = require ( 'util' ) ;
47
+ const { validateInternalField } = require ( 'internal/validators' ) ;
43
48
44
49
const {
45
50
getTimeOriginTimestamp
@@ -63,177 +68,147 @@ class Performance extends EventTarget {
63
68
timeOrigin : this . timeOrigin ,
64
69
} , opts ) } `;
65
70
}
66
- }
67
71
68
- function toJSON ( ) {
69
- return {
70
- nodeTiming : this . nodeTiming ,
71
- timeOrigin : this . timeOrigin ,
72
- eventLoopUtilization : this . eventLoopUtilization ( )
73
- } ;
74
- }
72
+ clearMarks ( name ) {
73
+ if ( name !== undefined ) {
74
+ name = ` ${ name } ` ;
75
+ }
76
+ clearMarkTimings ( name ) ;
77
+ clearEntriesFromBuffer ( 'mark' , name ) ;
78
+ }
75
79
76
- function clearMarks ( name ) {
77
- if ( name !== undefined ) {
78
- name = `${ name } ` ;
80
+ clearMeasures ( name ) {
81
+ if ( name !== undefined ) {
82
+ name = `${ name } ` ;
83
+ }
84
+ clearEntriesFromBuffer ( 'measure' , name ) ;
79
85
}
80
- clearMarkTimings ( name ) ;
81
- clearEntriesFromBuffer ( 'mark' , name ) ;
82
- }
83
86
84
- function clearMeasures ( name ) {
85
- if ( name !== undefined ) {
86
- name = `${ name } ` ;
87
+ clearResourceTimings ( name = undefined ) {
88
+ if ( name !== undefined ) {
89
+ name = `${ name } ` ;
90
+ }
91
+ clearEntriesFromBuffer ( 'resource' , name ) ;
92
+ }
93
+
94
+ getEntries ( ) {
95
+ return filterBufferMapByNameAndType ( ) ;
87
96
}
88
- clearEntriesFromBuffer ( 'measure' , name ) ;
89
- }
90
97
91
- function clearResourceTimings ( name ) {
92
- if ( name !== undefined ) {
98
+ getEntriesByName ( name ) {
99
+ if ( arguments . length === 0 ) {
100
+ throw new ERR_MISSING_ARGS ( 'name' ) ;
101
+ }
93
102
name = `${ name } ` ;
103
+ return filterBufferMapByNameAndType ( name , undefined ) ;
94
104
}
95
- clearEntriesFromBuffer ( 'resource' , name ) ;
96
- }
97
105
98
- function getEntries ( ) {
99
- return filterBufferMapByNameAndType ( ) ;
100
- }
106
+ getEntriesByType ( type ) {
107
+ if ( arguments . length === 0 ) {
108
+ throw new ERR_MISSING_ARGS ( 'type' ) ;
109
+ }
110
+ type = `${ type } ` ;
111
+ return filterBufferMapByNameAndType ( undefined , type ) ;
112
+ }
101
113
102
- function getEntriesByName ( name ) {
103
- if ( arguments . length === 0 ) {
104
- throw new ERR_MISSING_ARGS ( 'name' ) ;
114
+ mark ( name , options = kEmptyObject ) {
115
+ return mark ( name , options ) ;
105
116
}
106
- name = `${ name } ` ;
107
- return filterBufferMapByNameAndType ( name , undefined ) ;
108
- }
109
117
110
- function getEntriesByType ( type ) {
111
- if ( arguments . length === 0 ) {
112
- throw new ERR_MISSING_ARGS ( 'type' ) ;
118
+ measure ( name , startOrMeasureOptions , endMark ) {
119
+ return measure ( name , startOrMeasureOptions , endMark ) ;
113
120
}
114
- type = `${ type } ` ;
115
- return filterBufferMapByNameAndType ( undefined , type ) ;
116
- }
117
121
118
- class InternalPerformance extends EventTarget { }
119
- InternalPerformance . prototype . constructor = Performance . prototype . constructor ;
120
- ObjectSetPrototypeOf ( InternalPerformance . prototype , Performance . prototype ) ;
122
+ now ( ) {
123
+ validateInternalField ( this , kPerformanceBrand , 'Performance' ) ;
124
+ return now ( ) ;
125
+ }
126
+
127
+ setResourceTimingBufferSize ( maxSize ) {
128
+ return setResourceTimingBufferSize ( maxSize ) ;
129
+ }
130
+
131
+ get timeOrigin ( ) {
132
+ validateInternalField ( this , kPerformanceBrand , 'Performance' ) ;
133
+ return getTimeOriginTimestamp ( ) ;
134
+ }
135
+
136
+ toJSON ( ) {
137
+ validateInternalField ( this , kPerformanceBrand , 'Performance' ) ;
138
+ return {
139
+ nodeTiming : this . nodeTiming ,
140
+ timeOrigin : this . timeOrigin ,
141
+ eventLoopUtilization : this . eventLoopUtilization ( )
142
+ } ;
143
+ }
144
+ }
121
145
122
146
ObjectDefineProperties ( Performance . prototype , {
123
- clearMarks : {
124
- __proto__ : null ,
125
- configurable : true ,
147
+ clearMarks : kEnumerableProperty ,
148
+ clearMeasures : kEnumerableProperty ,
149
+ clearResourceTimings : kEnumerableProperty ,
150
+ getEntries : kEnumerableProperty ,
151
+ getEntriesByName : kEnumerableProperty ,
152
+ getEntriesByType : kEnumerableProperty ,
153
+ mark : kEnumerableProperty ,
154
+ measure : kEnumerableProperty ,
155
+ now : kEnumerableProperty ,
156
+ timeOrigin : kEnumerableProperty ,
157
+ toJSON : kEnumerableProperty ,
158
+ setResourceTimingBufferSize : kEnumerableProperty ,
159
+ [ SymbolToStringTag ] : {
160
+ __proto__ : null ,
161
+ writable : false ,
126
162
enumerable : false ,
127
- value : clearMarks ,
128
- } ,
129
- clearMeasures : {
130
- __proto__ : null ,
131
163
configurable : true ,
132
- enumerable : false ,
133
- value : clearMeasures ,
134
- } ,
135
- clearResourceTimings : {
136
- __proto__ : null ,
137
- configurable : true ,
138
- enumerable : false ,
139
- value : clearResourceTimings ,
164
+ value : 'Performance' ,
140
165
} ,
166
+
167
+ // Node.js specific extensions.
141
168
eventLoopUtilization : {
142
169
__proto__ : null ,
143
170
configurable : true ,
171
+ // Node.js specific extensions.
144
172
enumerable : false ,
173
+ writable : true ,
145
174
value : eventLoopUtilization ,
146
175
} ,
147
- getEntries : {
148
- __proto__ : null ,
149
- configurable : true ,
150
- enumerable : false ,
151
- value : getEntries ,
152
- } ,
153
- getEntriesByName : {
154
- __proto__ : null ,
155
- configurable : true ,
156
- enumerable : false ,
157
- value : getEntriesByName ,
158
- } ,
159
- getEntriesByType : {
160
- __proto__ : null ,
161
- configurable : true ,
162
- enumerable : false ,
163
- value : getEntriesByType ,
164
- } ,
165
- mark : {
166
- __proto__ : null ,
167
- configurable : true ,
168
- enumerable : false ,
169
- value : mark ,
170
- } ,
171
- measure : {
172
- __proto__ : null ,
173
- configurable : true ,
174
- enumerable : false ,
175
- value : measure ,
176
- } ,
177
176
nodeTiming : {
178
177
__proto__ : null ,
179
178
configurable : true ,
179
+ // Node.js specific extensions.
180
180
enumerable : false ,
181
+ writable : true ,
181
182
value : nodeTiming ,
182
183
} ,
183
184
// In the browser, this function is not public. However, it must be used inside fetch
184
185
// which is a Node.js dependency, not a internal module
185
186
markResourceTiming : {
186
187
__proto__ : null ,
187
188
configurable : true ,
189
+ // Node.js specific extensions.
188
190
enumerable : false ,
191
+ writable : true ,
189
192
value : markResourceTiming ,
190
193
} ,
191
- now : {
192
- __proto__ : null ,
193
- configurable : true ,
194
- enumerable : false ,
195
- value : now ,
196
- } ,
197
- setResourceTimingBufferSize : {
198
- __proto__ : null ,
199
- configurable : true ,
200
- enumerable : false ,
201
- value : setResourceTimingBufferSize
202
- } ,
203
194
timerify : {
204
195
__proto__ : null ,
205
196
configurable : true ,
197
+ // Node.js specific extensions.
206
198
enumerable : false ,
199
+ writable : true ,
207
200
value : timerify ,
208
201
} ,
209
- // This would be updated during pre-execution in case
210
- // the process is launched from a snapshot.
211
- // TODO(joyeecheung): we may want to warn about access to
212
- // this during snapshot building.
213
- timeOrigin : {
214
- __proto__ : null ,
215
- configurable : true ,
216
- enumerable : true ,
217
- value : getTimeOriginTimestamp ( ) ,
218
- } ,
219
- toJSON : {
220
- __proto__ : null ,
221
- configurable : true ,
222
- enumerable : true ,
223
- value : toJSON ,
224
- }
225
202
} ) ;
226
203
227
- function refreshTimeOrigin ( ) {
228
- ObjectDefineProperty ( Performance . prototype , 'timeOrigin' , {
229
- __proto__ : null ,
230
- configurable : true ,
231
- enumerable : true ,
232
- value : getTimeOriginTimestamp ( ) ,
233
- } ) ;
204
+ function createPerformance ( ) {
205
+ return ReflectConstruct ( function Performance ( ) {
206
+ initEventTarget ( this ) ;
207
+ this [ kPerformanceBrand ] = true ;
208
+ } , [ ] , Performance ) ;
234
209
}
235
210
236
- const performance = new InternalPerformance ( ) ;
211
+ const performance = createPerformance ( ) ;
237
212
238
213
function dispatchBufferFull ( type ) {
239
214
const event = new Event ( type , {
@@ -246,5 +221,4 @@ setDispatchBufferFull(dispatchBufferFull);
246
221
module . exports = {
247
222
Performance,
248
223
performance,
249
- refreshTimeOrigin
250
224
} ;
0 commit comments