marky

High-resolution JavaScript timer based on performance.mark/measure (491 byt...

README

marky
======

JavaScript timer based on performance.mark() and performance.measure(), providing [high-resolution
timings](https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API) as well as nice Dev Tools visualizations.

For browsers that don't support performance.mark(), it falls back to
performance.now() or Date.now(). In Node, it uses process.hrtime().

Quick start

Install via npm:

    npm install marky

Or as a script tag:

  1. ``` html
  2. <script src="https://unpkg.com/marky/dist/marky.min.js"></script>
  3. ```

Then take some measurements:

  1. ``` js
  2. var marky = require('marky');

  3. marky.mark('expensive operation');
  4. doExpensiveOperation();
  5. marky.stop('expensive operation');
  6. ```

Why?

than console.time() and console.timeEnd(),
and more accurate thanDate.now(). Also, you get nice visualizations in Chrome Dev Tools:

Chrome Dev Tools screenshot

As well as Edge F12 Tools:

Edge F12 screenshot

This is because marky adds standard

API

marky.mark() begins recording, and marky.stop() finishes recording:

  1. ``` js
  2. marky.mark('releaseTheHounds');
  3. releaseTheHounds();
  4. marky.stop('releaseTheHounds');
  5. ```

You can also do more complex scenarios:

  1. ``` js
  2. function setSail() {
  3.   marky.mark('setSail');
  4.   marky.mark('raiseTheAnchor');
  5.   raiseTheAnchor();
  6.   marky.stop('raiseTheAnchor');
  7.   marky.mark('unfurlTheSails');
  8.   unfurlTheSails();
  9.   marky.stop('unfurlTheSails');
  10.   marky.stop('setSail');
  11. }
  12. ```

marky.stop() also returns a PerformanceEntry:

  1. ``` js
  2. marky.mark('manTheTorpedos');
  3. manTheTorpedos();
  4. var entry = marky.stop('manTheTorpedos');
  5. ```

The entry will look something like:

  1. ``` json
  2. {
  3.   "entryType": "measure",
  4.   "startTime": 1974112,
  5.   "duration": 350,
  6.   "name": "manTheTorpedos"
  7. }
  8. ```

You can get all entries using:

  1. ``` js
  2. var entries = marky.getEntries();
  3. ```

This provides a list of all measures ordered by startTime, e.g.:

  1. ``` json
  2. [
  3.   {
  4.     "entryType": "measure",
  5.     "startTime": 1974112,
  6.     "duration": 350,
  7.     "name": "numberOne"
  8.   },
  9.   {
  10.     "entryType": "measure",
  11.     "startTime": 1975108,
  12.     "duration": 300,
  13.     "name": "numberTwo"
  14.   },
  15.   {
  16.     "entryType": "measure",
  17.     "startTime": 1976127,
  18.     "duration": 250,
  19.     "name": "numberThree"
  20.   }
  21. ]
  22. ```

You can also clear the entries using marky.clear():

  1. ``` js
  2. marky.clear()
  3. ```

After this, marky.getEntries() will return an empty list. (If the User Timing API is supported, this will delete all the mark and measure entries from the timeline.)

Browser support

marky has been tested in the following browsers/environments:

IE 9+
Safari 8+
iOS 8+
Android 4.4+
Chrome
Firefox
Edge
Node 4+

Per the spec, browsers only need to hold a minimum
of 150 entries in their Performance Timeline buffer. In older versions of Firefox, the buffer
is throttled to 150, which for marky
means you can get a maximum of 50 entries from marky.getEntries() (because marky creates two marks and a measure).

If you need to get more than 50 entries from marky.getEntries(), you can do:

  1. ``` js
  2. if (typeof performance !== 'undefined' && performance.setResourceTimingBufferSize) {
  3.   performance.setResourceTimingBufferSize(10000); // or however many you need
  4. }
  5. ```

marky follows the behavior of Edge and Chrome, and does not limit the number of entries. marky.stop() and
marky.getEntries() will return pseudo-PerformanceEntry objects.

See also

- appmetrics.js – a library on top ofmark()/measure() which reports to Google Analytics.

Credits

Thanks to @toddreifsteck for feedback on this project and clarifications on the User Timing API.