UNPKG

2.71 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')
11var timing = nanotiming('my-timing')
12
13// make sure the buffer doesn't overflow, set this once per application
14if (typeof window !== 'undefined' &&
15 window.performance &&
16 window.performance.getEntriesByName) {
17 window.performance.onresourcetimingbufferfull = function () {
18 window.performance.clearResourceTimings()
19 }
20}
21
22timing.start('my-loop')
23var i = 1000
24while (--i) console.log(i)
25timing.end('my-loop')
26
27var timings = window.performance.getEntriesByName('my-timing:my-loop')
28console.log(timings[timings.length - 1]) // log the last entry
29```
30
31## API
32### `timing = nanotiming([instanceName])`
33Create a new Nanotiming instance. Takes a name for the timing instance.
34
35### `uuid = timing.start([methodName])`
36Start a timing. Takes a method name. The method name is concatenated to the
37instance name as `<instanceName>/<methodName>`. If no method name is passed,
38it'll fall back to only using the instance name. It's recommended that per
39instance to either always use method names, or never.
40
41Returns a `uuid` that should be passed to `timing.end()` so async timings work.
42
43### `timing.end(uuid, [methodName])`
44End a timing. The name here must be the same as `timing.start()`. If using a
45static name, the `timing.end()` call must resolve on the same tick as
46`timing.start()` to prevent race conditions. If you want to do use this in
47async conditions make sure the name is unique, for example by appending a
48unique id to both start and end.
49
50Takes a timing that is povided by `timing.start()` so async timings work.
51
52## License
53[MIT](https://tldrlegal.com/license/mit-license)
54
55[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
56[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
57[2]: https://img.shields.io/npm/v/nanotiming.svg?style=flat-square
58[3]: https://npmjs.org/package/nanotiming
59[4]: https://img.shields.io/travis/yoshuawuyts/nanotiming/master.svg?style=flat-square
60[5]: https://travis-ci.org/yoshuawuyts/nanotiming
61[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/nanotiming/master.svg?style=flat-square
62[7]: https://codecov.io/github/yoshuawuyts/nanotiming
63[8]: http://img.shields.io/npm/dm/nanotiming.svg?style=flat-square
64[9]: https://npmjs.org/package/nanotiming
65[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
66[11]: https://github.com/feross/standard