UNPKG

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