UNPKG

2.62 kBMarkdownView Raw
1# nanotiming [![stability][0]][1]
2[![npm version][2]][3] [![build status][4]][5]
3[![downloads][8]][9] [![js-standard-style][10]][11]
4
5Small timing library. Useful to integrate into libraries that have multiple
6methods. Only works in the browser, does nothing in Node.
7
8## Usage
9```js
10var nanotiming = require('nanotiming')
11
12var timing = nanotiming('my-loop') // Start profiling
13
14var i = 1000
15while (--i) console.log(i)
16
17// Stop profiling
18timing(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
32Timings inside the view are appended with a unique UUID so they can be cleared
33individually. While there's no strict format for timing formats, we recommend
34using a format along these lines:
35```txt
36choo.render [12356778]
37choo.route('/') [13355671]
38choo.emit('log:debug') [13355675]
39```
40
41## Disabling timings
42Performance timers are still a somewhat experimental technology. While they're
43a great idea conceptually, there might be bugs. To disable timings complete,
44set:
45```js
46window.localStorage.DISABLE_NANOTIMING = true
47```
48
49## API
50### `endTiming = nanotiming(name)`
51Start a new timing.
52
53### `endTiming.uuid`
54The unique ID created for the timing.
55
56### `endTiming([cb(err, name)])`
57Close the timing. Measuring the timing is done inside a `requestIdleCallback()`
58tick, so it might not be available immediately. If a callback is passed it will
59be 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