-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx.js
65 lines (49 loc) · 1.28 KB
/
x.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
const {
duration,
stopwatch,
time: timeSync,
timeAsync,
timePromised
} = require('./lib/index')
const nanoseconds = 987654321
console.log("Duration is", duration(nanoseconds).format())
// Or, since toString() is an alias to format()
console.log(`Duration is ${duration(nanoseconds)}`)
const watch = stopwatch()
// Pauses the stopwatch. Returns the stopwatch.
watch.stop()
// Starts the stopwatch from where it was last stopped. Returns the stopwatch.
watch.start()
// Reset the stopwatch (duration is set back to zero). Returns the stopwatch.
watch.reset()
console.log(`${watch.duration().seconds()} seconds have elapsed`)
// OR
console.log(`${watch} have elapsed`)
// Synchronous work
const someFunction = () => {
let count = 0
while (count < 1000000) {
count++
}
console.log(`Count is: ${count}`)
}
console.log(`Took ${timeSync(someFunction)} to do something`)
// Asynchronous work
const someOtherFunction = next => {
someFunction()
next()
}
timeAsync(someOtherFunction, duration => {
console.log(`Took ${duration} to do something else.`)
})
// Promised work
const somePromisedOp = () => {
return new Promise((resolve) => {
someFunction()
resolve()
})
}
timePromised(somePromisedOp)
.then(duration => {
console.log(`Took ${duration} to keep promise.`)
})