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