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 | var timing = nanotiming('my-timing')
|
12 |
|
13 | // make sure the buffer doesn't overflow, set this once per application
|
14 | if (typeof window !== 'undefined' &&
|
15 | window.performance &&
|
16 | window.performance.getEntriesByName) {
|
17 | window.performance.onresourcetimingbufferfull = function () {
|
18 | window.performance.clearResourceTimings()
|
19 | }
|
20 | }
|
21 |
|
22 | timing.start('my-loop')
|
23 | var i = 1000
|
24 | while (--i) console.log(i)
|
25 | timing.end('my-loop')
|
26 |
|
27 | var timings = window.performance.getEntriesByName('my-timing:my-loop')
|
28 | console.log(timings[timings.length - 1]) // log the last entry
|
29 | ```
|
30 |
|
31 | ## API
|
32 | ### `timing = nanotiming([instanceName])`
|
33 | Create a new Nanotiming instance. Takes a name for the timing instance.
|
34 |
|
35 | ### `uuid = timing.start([methodName])`
|
36 | Start a timing. Takes a method name. The method name is concatenated to the
|
37 | instance name as `<instanceName>/<methodName>`. If no method name is passed,
|
38 | it'll fall back to only using the instance name. It's recommended that per
|
39 | instance to either always use method names, or never.
|
40 |
|
41 | Returns a `uuid` that should be passed to `timing.end()` so async timings work.
|
42 |
|
43 | ### `timing.end(uuid, [methodName])`
|
44 | End a timing. The name here must be the same as `timing.start()`. If using a
|
45 | static 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
|
47 | async conditions make sure the name is unique, for example by appending a
|
48 | unique id to both start and end.
|
49 |
|
50 | Takes 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
|