Giter VIP home page Giter VIP logo

statman-stopwatch's Introduction

statman-stopwatch

Build Status on npm Greenkeeper Dependency badge Codacy Badge

statman-stopwatch is one of the metrics from the statman library. It is a simple high res stopwatch for node.js. Stopwatch is useful for determining the amount of time it takes to perform an activity.

For example, you may want to determine how long certain potentially expensive activities take in your code (such as calling to an external web services or fetching a dataset from a database). Few lines of code will let you capture that info. There are much more elegant solutions - this is a simple roll-your-own approach.

Install it!

Option 1: access directly

Install using npm:

npm install statman-stopwatch

Reference in your app:

const Stopwatch = require('statman-stopwatch');
const stopwatch = new Stopwatch();

Option 2: access from statman

Install using npm:

npm install statman

Reference in your app:

const statman = require('statman');
const stopwatch = new statman.Stopwatch();

Use it!

Constructor

  • Stopwatch() => create instance of a stopwatch
  • Stopwatch(true) => create instance of stopwatch, and have it autostart
  • `Stopwatch(name, autostart, delta) => create instance of stopwatch, with name, specify if to autostart, and supply an automatic delta (see setStartTimeDelta)

start

  • start() => starts the stopwatch, let the timing begin!

read

  • read(precision) => reads the stopwatch to determine how much time has elapsed. Note that the stopwatch continues to run. Returns the time elapsed in milliseconds. If precision is provided, read() will round to the numbe of decimals places based on precision.
  • time(precision) => alias for read()

stop

  • stop() => stops the stopwatch, and returns the time elapsed in milliseconds

split

  • split() => temp stops the stopwatch, allow read() to return time based on when split occurs. Use unsplit() to resume the stopwatch

unsplit

  • unsplit() => use follow a split() to resume the stopwatch

splitTime

  • splitTime => while the stopwatch is split, returns the time as of the split

reset

  • reset() => restores the stopwatch back to init state and clears start and stop times

setStartTimeDelta

  • setStartTimeDelta(number) => provide an elapsed time (in milliseconds) at which to start the stopwatch

Example

There are some examples in example/example.js

Basic usage

Create a new stopwatch, start() it, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch();
    sw.start();

    // do some activity

    const delta = sw.read();

Autostart

start() is too hard. Create a new stopwatch with autostart=true, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch(true);

    // do some activity

    const delta = sw.read();

Stop

Create a new stopwatch, stop() it, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch(true);

    // do some activity

    sw.stop();

    // do some more activity

    //returns time associated with when stop() occurred
    const delta = sw.read();

Delta

Create a new stopwatch, start() it, and later read() it

    const Stopwatch = require('statman-stopwatch');
    const sw = new Stopwatch();
    sw.setStartTimeDelta(5000);
    sw.start();

    // do some activity which takes 500

    const delta = sw.read();
    // delta will be 5500 (the initial 5000ms set in setStartTimeDelta plus the elapsed 500ms)

Build it!

  • Make sure that you have node and npm installed
  • Clone source code to you local machine
  • Setup dependencies: npm install
  • run tests: npm test

statman-stopwatch's People

Contributors

jasonray avatar kuc avatar codacy-badger avatar greenkeeper[bot] avatar

Watchers

James Cloos 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.