GithubHelp home page GithubHelp logo

alexxnica / marky Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nolanlawson/marky

0.0 1.0 0.0 150 KB

High-resolution JavaScript timer based on performance.mark/measure (461 bytes min+gz)

License: Apache License 2.0

JavaScript 100.00%

marky's Introduction

marky Build Status

JavaScript timer based on performance.mark() and performance.measure(), providing high-resolution timings 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:

<script src="https://unpkg.com/marky/dist/marky.min.js"></script>

Then take some measurements:

var marky = require('marky');

marky.mark('expensive operation');
doExpensiveOperation();
marky.stop('expensive operation');

Why?

The User Timing API is more performant than console.time() and console.timeEnd(), and more accurate than Date.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 PerformanceEntries to the Performance Timeline. Tools like WebPageTest and Windows Performance Analyzer also surface them, and you can even send them to your analytics provider.

API

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

marky.mark('releaseTheHounds');
releaseTheHounds();
marky.stop('releaseTheHounds');

You can also do more complex scenarios:

function setSail() {
  marky.mark('setSail');
  marky.mark('raiseTheAnchor');
  raiseTheAnchor();
  marky.stop('raiseTheAnchor');
  marky.mark('unfurlTheSails');
  unfurlTheSails();
  marky.stop('unfurlTheSails');
  marky.stop('setSail');
}

marky.stop() also returns a PerformanceEntry:

marky.mark('manTheTorpedos');
manTheTorpedos();
var entry = marky.stop('manTheTorpedos');

The entry will look something like:

{
  "entryType": "measure",
  "startTime": 1974112,
  "duration": 350,
  "name": "manTheTorpedos"
}

You can get all entries using:

var entries = marky.getEntries();

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

[
  {
    "entryType": "measure",
    "startTime": 1974112,
    "duration": 350,
    "name": "numberOne"
  },
  {
    "entryType": "measure",
    "startTime": 1975108,
    "duration": 300,
    "name": "numberTwo"
  },
  {
    "entryType": "measure",
    "startTime": 1976127,
    "duration": 250,
    "name": "numberThree"
  }
]

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

marky.clear()

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 is 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. Notably Firefox throttles their buffer 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:

if (typeof performance !== 'undefined' && performance.setResourceTimingBufferSize) {
  performance.setResourceTimingBufferSize(10000); // or however many you need
}

In Node and browsers that don't support the User Timing API, 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 of mark()/measure() which reports to Google Analytics.

Credits

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

marky's People

Contributors

nolanlawson avatar oaleynik avatar paulirish avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.