-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.js
128 lines (117 loc) · 3.01 KB
/
stopwatch.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
class Stopwatch {
constructor() {
this.start =
typeof process !== 'undefined' && process.hrtime
? this.nodeStart
: typeof window !== 'undefined' && window.performance
? this.browserStart
: () => {}
this.end =
typeof process !== 'undefined' && process.hrtime
? this.nodeEnd
: typeof window !== 'undefined' && window.performance
? this.browserEnd
: () => {}
}
nodeStart() {
this.timer = process.hrtime()
}
nodeEnd() {
let elapsed = process.hrtime(this.timer)
return round3(elapsed[0] * 1000 + elapsed[1] / 1000000) // Convert to ms, hrtime returns full seconds in [0] and nanoseconds in [1]
}
browserStart() {
this.startTime = window.performance.now()
}
browserEnd() {
let elapsed = window.performance.now() - this.startTime
return round3(elapsed)
}
log() {
console.log(this.end() + ' ms')
}
get test() {
return Stopwatch.test
}
get stop() {
return this.end
}
static test(
callback,
{ loops = 1, preparation = undefined, silent = false, label = '' } = {}
) {
const preparedData =
typeof preparation === 'function' ? preparation() : preparation
const sw = new Stopwatch()
sw.start()
for (let index = 0; index < loops; index++) {
callback(index, preparedData)
}
let total = sw.end()
// @ts-ignore
let average = total / loops
if (!silent) {
console.log(
(label ? '\x1b[36m' + label + ': \x1b[0m' : '') +
'Average: ' +
average +
' ms, total: ' +
total +
' ms'
)
}
return [total, average]
}
static async testAsync(
callback,
{ loops = 1, preparation = undefined, silent = false, label = '' } = {}
) {
const preparedData =
typeof preparation === 'function' ? preparation() : preparation
const sw = new Stopwatch()
sw.start()
for (let index = 0; index < loops; index++) {
await callback(index, preparedData)
}
let total = sw.end()
// @ts-ignore
let average = total / loops
if (!silent) {
console.log(
(label ? '\x1b[36m' + label + ': \x1b[0m' : '') +
'Average: ' +
average +
' ms, total: ' +
total +
' ms'
)
}
return [total, average]
}
static decorate(fn, { label = '', queueLog = false } = {}) {
return (...args) => {
const sw = new Stopwatch()
sw.start()
const result = fn(...args)
const time = sw.end()
const logFn = () => {
console.log(
(label ? '\x1b[36m' + label + ': \x1b[0m' : '') +
'Execution time: ' +
time +
' ms'
)
}
if (!queueLog) {
logFn()
} else {
setImmediate(logFn)
}
return result
}
}
}
function round3(number) {
return Math.round(number * 1000) / 1000 // Add something like 0.00001 after multiplication if floating point errors is an issue
}
module.exports = Stopwatch