1 | # nanotiming [![stability][0]][1]
|
2 | [![npm version][2]][3] [![build status][4]][5]
|
3 | [![downloads][8]][9] [![js-standard-style][10]][11]
|
4 |
|
5 | Small timing library. Useful to integrate into libraries that have multiple
|
6 | methods. Only works in the browser, does nothing in Node.
|
7 |
|
8 | ## Usage
|
9 | ```js
|
10 | var nanotiming = require('nanotiming')
|
11 |
|
12 | var timing = nanotiming('my-loop') // Start profiling
|
13 |
|
14 | var i = 1000
|
15 | while (--i) console.log(i)
|
16 |
|
17 | // Stop profiling
|
18 | timing(function (err) {
|
19 | if (err) return
|
20 |
|
21 | // Inspect timings when we have spare time available
|
22 | window.requestIdleCallback(function () {
|
23 | var timings = window.performance.getEntries()
|
24 | var timing = timings[timings.length - 1]
|
25 | console.log(timing.name, timing.duration) // log the last entry
|
26 | performance.clearMeasures(timing.name) // be a good citizen and free after use
|
27 | })
|
28 | })
|
29 | ```
|
30 |
|
31 | ## Timing names
|
32 | Timings inside the view are appended with a unique UUID so they can be cleared
|
33 | individually. While there's no strict format for timing formats, we recommend
|
34 | using a format along these lines:
|
35 | ```txt
|
36 | choo.render [12356778]
|
37 | choo.route('/') [13355671]
|
38 | choo.emit('log:debug') [13355675]
|
39 | ```
|
40 |
|
41 | ## Disabling timings
|
42 | Performance timers are still a somewhat experimental technology. While they're
|
43 | a great idea conceptually, there might be bugs. To disable timings complete,
|
44 | set:
|
45 | ```js
|
46 | window.localStorage.DISABLE_NANOTIMING = true
|
47 | ```
|
48 |
|
49 | ## API
|
50 | ### `endTiming = nanotiming(name)`
|
51 | Start a new timing.
|
52 |
|
53 | ### `endTiming.uuid`
|
54 | The unique ID created for the timing.
|
55 |
|
56 | ### `endTiming([cb(err, name)])`
|
57 | Close the timing. Measuring the timing is done inside a `requestIdleCallback()`
|
58 | tick, so it might not be available immediately. If a callback is passed it will
|
59 | be called with an error (if measuring wasn't successful) and the timing's name.
|
60 |
|
61 | ## License
|
62 | [MIT](https://tldrlegal.com/license/mit-license)
|
63 |
|
64 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
|
65 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index
|
66 | [2]: https://img.shields.io/npm/v/nanotiming.svg?style=flat-square
|
67 | [3]: https://npmjs.org/package/nanotiming
|
68 | [4]: https://img.shields.io/travis/choojs/nanotiming/master.svg?style=flat-square
|
69 | [5]: https://travis-ci.org/choojs/nanotiming
|
70 | [6]: https://img.shields.io/codecov/c/github/choojs/nanotiming/master.svg?style=flat-square
|
71 | [7]: https://codecov.io/github/choojs/nanotiming
|
72 | [8]: http://img.shields.io/npm/dm/nanotiming.svg?style=flat-square
|
73 | [9]: https://npmjs.org/package/nanotiming
|
74 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
|
75 | [11]: https://github.com/feross/standard
|