-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-timer.js
407 lines (380 loc) · 13.5 KB
/
process-timer.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* @class ProcessTimer
* @provides High-resolution timer class for NodeJs & browsers
* Implements timestamps processing in a handy simple way
*
* @author https://juliyvchirkov.github.io
* @version 1.0.2 17/08/2018
* @release https://github.com/juliyvchirkov/process-timer/releases/tag/v1.0.2
* @bugs https://github.com/juliyvchirkov/process-timer/issues
* @license MIT
*
* @see README.md
*
* ProcessTimer {
* VERSION : '1.0.2',
* nsec : [Getter],
* usec : [Getter],
* msec : [Getter],
* sec : [Getter]
* ns : [Getter],
* us : [Getter],
* ms : [Getter],
* s : [Getter]
* }
*/
'use strict'
/**
* Universal module definition, turned into one-liner for simplicity & conciseness
*
* @see https://github.com/umdjs/umd
*/
;(function (global, factory) {
typeof define === 'function' && define.amd
? define(factory)
: typeof module === 'object' && module.exports
? (module.exports = factory)
: (global.ProcessTimer = factory)
})(this, function ProcessTimer (suffix) {
'use strict'
var CLASSID = 'ProcessTimer'
var VERSION = '1.0.2'
/**
* Defines or modifies a property of an object
*
* A TypeError is thrown on failure
*
* @param {object} object The object on which to define the property
* @param {string} property The name of the property to be defined or modified
* @param {object} descriptor The descriptor for the property being defined or
* modified
*
* @returns {object} The object that was passed to the function
*/
function defineProperty (object, property, descriptor) {
var data = 'value'
var accessorGet = 'get'
var accessorSet = 'set'
var errorPrefix = 'Cannot define or modify property “' + property + '” '
var errorMessage = null
/**
* Defines or modifies an accessor property of an object
*
* @param {string} id The name of the accessor property to be defined
* or modified (either “get” or “set”)
*
* @returns {void}
*/
function defineAccessor (id) {
var __define__ = '__define' + id[0].toUpperCase() + id.slice(1) + 'ter__'
var __lookup__ = '__lookup' + __define__.slice(8)
errorMessage = errorPrefix + id + 'ter'
try {
object[__define__](property, descriptor[id])
if (object[__lookup__](property) === descriptor[id]) {
errorMessage = null
}
} catch (error) {}
}
try {
Object.defineProperty(object, property, descriptor)
} catch (error) {
if (data in descriptor) {
try {
object[property] = descriptor[data]
} catch (error) {
} finally {
if (object[property] !== descriptor[data]) {
errorMessage = errorPrefix + data
}
}
} else {
accessorGet in descriptor && defineAccessor(accessorGet)
accessorSet in descriptor && defineAccessor(accessorSet)
}
} finally {
if (errorMessage) {
throw new TypeError(errorMessage)
} else {
return object
}
}
}
/**
* Defines or modifies properties of an object
*
* A TypeError is thrown on failure
*
* @param {object} object The object on which to define properties
* @param {object} properties An object whose own enumerable properties
* constitute descriptors for the properties
* to be defined or modified
*
* @returns {object} The object that was passed to the function
*/
function defineProperties (object, properties) {
var errorMessage = null
try {
Object.defineProperties(object, properties)
} catch (error) {
try {
for (var property in properties) {
defineProperty(object, property, properties[property])
}
} catch (error) {
errorMessage = error.message
}
} finally {
if (errorMessage) {
throw new TypeError(errorMessage)
} else {
return object
}
}
}
/**
* Makes an object immutable if Object.freeze method is available
*
* @param {object} object The object to freeze
*
* @returns {object} The object that was passed to the function
*/
function freeze (object) {
try {
Object.freeze(object)
} catch (error) {
} finally {
return object
}
}
/**
* Returns a string indicating the type of an object or a primitive
*
* A TypeError is thrown if invoked w/o arguments
*
* @param { … } item The object or primitive whose type is to be determined
*
* @returns {string} The type of the item that was passed to the function
*/
function typeOf (item) {
if (arguments.length === 0) {
throw new TypeError('Object or primitive has been expected, got nothing')
}
var itemTypeof = null
try {
itemTypeof = item.constructor.name.toLowerCase()
} catch (error) {
} finally {
return itemTypeof && itemTypeof !== typeof item
? itemTypeof
: Object.prototype.toString.call(item).slice(8, -1).toLowerCase()
}
}
/**
* Returns a timer startpoint (a [seconds, nanoseconds] array tuple w/
* unique timestamp) if invoked w/o arguments. Further this array being
* passed back produces a diff (a [seconds, nanoseconds] array tuple w/
* time elapsed since a timer has been instantiated) on return
*
* A TypeError is thrown on anything but an array tuple
*
* @param {array} time The return of this function called w/o arguments
*
* @returns {array} A high-resolution timestamp in a [seconds,
* nanoseconds] array tuple, where nanoseconds
* is the remaining part of the timestamp that
* can't be represented in second precision
*/
function hrtime (time) {
try {
return process.hrtime(time)
} catch (error) {
time = time || [0, 0]
var timeTypeof = typeOf(time)
var timeTypeofArray = timeTypeof === 'array'
if (timeTypeofArray && time.length === 2) {
var now =
(typeof performance === 'object' &&
typeOf(performance) === 'performance' &&
typeOf(performance.now) === 'function'
? performance
: Date
).now() * 1e-3
var sec = Math.floor(now) - time[0]
var nsec = Math.floor((now % 1) * 1e9) - time[1]
var shift = (nsec < 0) * 1
return [sec - shift, nsec + shift * 1e9]
} else {
throw new TypeError(
'Array tuple has been expected, got ' +
(timeTypeofArray ? 'incompatible one' : timeTypeof)
)
}
}
}
/**
* The above shims have been designed to sand off some rough edges between
* NodeJs & various browser engines thru a number of modern & legacy methods
* sequenced within single functions in order to keep the main routine utterly
* simple & concise
*/
var classConstructor = 'Class constructor ' + CLASSID + ' '
if (!(this instanceof ProcessTimer)) {
throw new TypeError(classConstructor + 'cannot be invoked w/o “new”')
}
if (arguments.length !== 0 && typeof suffix !== 'string') {
throw new TypeError('String has been expected, got ' + typeOf(suffix))
}
/**
* Converts an integer of a high-resolution timestamp into a double
* according to the given precision
*
* Utilized to process ns, us & ms getters
*
* @param {number} stamp An integer of a high-resolution timestamp
* @param {number} depth The precision (either 9 or 6 or 3)
*
* @returns {number} A double of a high-resolution timestamp that
* was passed to the function
*/
function getHRSec (stamp, depth) {
var precision = Math.pow(10, -depth)
var sec = (stamp * precision).toFixed(depth)
return (sec * 1 - (sec.slice(-1) === '0' ? precision : 0)).toFixed(depth) * 1
}
/**
* The main routine
*/
try {
return freeze(
defineProperties(this, {
VERSION : {
enumerable : true,
value : VERSION
},
/**
* A suffix to append the return of ns, us, ms & s getters
* (a string that was passed to the constructor if any, null
* otherwise)
*/
suffix : {
value : typeOf(suffix) === 'string' ? suffix : null
},
/**
* A timer startpoint (a [seconds, nanoseconds] array tuple
* w/ unique timestamp)
*/
time : {
value : freeze(hrtime())
},
/**
* @getter nsec
*
* @returns {number} A number of nanoseconds elapsed since
* a timer has been instantiated
*/
nsec : {
enumerable : true,
get : function get () {
var time = hrtime(this.time)
return time[0] * 1e9 + time[1]
}
},
/**
* @getter usec
*
* @returns {number} A number of microseconds elapsed since
* a timer has been instantiated
*/
usec : {
enumerable : true,
get : function get () {
return Math.round(this.nsec * 1e-3)
}
},
/**
* @getter msec
*
* @returns {number} A number of milliseconds elapsed since
* a timer has been instantiated
*/
msec : {
enumerable : true,
get : function get () {
return Math.round(this.nsec * 1e-6)
}
},
/**
* @getter sec
*
* @returns {number} A number of seconds elapsed since a timer
* has been instantiated
*/
sec : {
enumerable : true,
get : function get () {
return Math.round(this.nsec * 1e-9)
}
},
/**
* @getter ns
*
* @returns {number|string} A number of seconds (accurate to
* nanoseconds) elapsed since a timer
* has been instantiated
*/
ns : {
enumerable : true,
get : function get () {
return getHRSec(this.nsec, 9) + this.suffix
}
},
/**
* @getter us
*
* @returns {number|string} A number of seconds (accurate to
* microseconds) elapsed since a timer
* has been instantiated
*/
us : {
enumerable : true,
get : function get () {
return getHRSec(this.usec, 6) + this.suffix
}
},
/**
* @getter ms
*
* @returns {number|string} A number of seconds (accurate to
* milliseconds) elapsed since a timer
* has been instantiated
*/
ms : {
enumerable : true,
get : function get () {
return getHRSec(this.msec, 3) + this.suffix
}
},
/**
* @getter s
*
* @returns {number|string} A number of seconds elapsed since
* a timer has been instantiated
*/
s : {
enumerable : true,
get : function get () {
return this.sec + this.suffix
}
}
})
)
} catch (error) {
throw new TypeError(
classConstructor +
'is unable to provide an instance (' +
error.message +
')'
)
}
})