# exectimer

> Very simple module to calculate block execution time.

Latest version **2.2.2** (published 2019-10-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install exectimer
pnpm add exectimer
yarn add exectimer
bun add exectimer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.2 |
| Published | 2019-10-14 |
| First published | 2014-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 2 |
| Unpacked size | 29.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alexandru Savin |
| Maintainers | alexsavin |
| Keywords | time, timer, profiler, execution, performance, analysis, benchmark |

## Links

- npm: https://www.npmjs.com/package/exectimer
- Repository: https://github.com/alexandrusavin/exectimer
- Issues: https://github.com/alexandrusavin/exectimer/issues
- npm.io page: https://npm.io/package/exectimer

## Dependencies (2)

- [co](https://npm.io/package/co.md) ^4.6.0
- [browser-process-hrtime](https://npm.io/package/browser-process-hrtime.md) ^1.0.0

## Alternatives

- [@js-joda/timezone](https://npm.io/package/@js-joda/timezone.md) — 383.4K weekly downloads
- [chartjs-adapter-moment](https://npm.io/package/chartjs-adapter-moment.md) — 210.8K weekly downloads
- [strftime](https://npm.io/package/strftime.md) — 171.2K weekly downloads
- [vue-flatpickr-component](https://npm.io/package/vue-flatpickr-component.md) — 115.8K weekly downloads
- [timepicker](https://npm.io/package/timepicker.md) — 51.0K weekly downloads

## Recent versions

- 2.2.2 (latest) — 2019-10-14
- 2.2.1 — 2018-11-05
- 2.2.0 — 2018-04-14
- 2.1.0 — 2018-02-15
- 2.0.3 — 2017-02-21
- 2.0.2 — 2016-10-24
- 2.0.1 — 2016-10-22
- 2.0.0 — 2015-12-05
- 1.1.0 — 2015-09-27
- 1.0.2 — 2015-04-08
- 1.0.1 — 2015-03-13
- 1.0.0 — 2015-03-12
- 0.2.2 — 2014-11-08
- 0.2.1 — 2014-08-17
- 0.1.5 — 2014-02-21
- … 5 more at https://npm.io/package/exectimer/versions

## README

Description
-----------

[![Greenkeeper badge](https://badges.greenkeeper.io/alexandrusavin/exectimer.svg)](https://greenkeeper.io/) [![Build Status](https://travis-ci.org/alexandrusavin/exectimer.png?branch=master)](https://travis-ci.org/alexandrusavin/exectimer)

Very simple but powerful nodejs and browser module build to track execution time with a resolution of nanoseconds.

Install
-------

Simply run:
```
npm install exectimer
```

Usage
-----

### Example

#### Wrapping generator
```javascript
const t = require('exectimer');
const Tick = t.Tick;

const promises = [];
for(var i = 0; i < 10; i++) {
    const functionExecution = Tick.wrap(function* myFunction() {
        yield Promise.resolve(true);
    });

    promises.push(functionExecution);
}

// After all ticks are finished
Promise.all(promises).then(() => {
    // display the results
    var results = t.timers.myFunction;
    console.log(results.parse(results.duration())); // total duration of all ticks
    console.log(results.parse(results.min()));      // minimal tick duration
    console.log(results.parse(results.max()));      // maximal tick duration
    console.log(results.parse(results.mean()));     // mean tick duration
    console.log(results.parse(results.median()));   // median tick duration
});
```

#### Wrapping
```javascript
const t = require('exectimer');
const Tick = t.Tick;


for(var i = 0; i < 10; i++) {
    Tick.wrap(function myFunction(done) {
        setTimeout(function() {
            done();
        }, 10);
    });
}

// Display the results
var results = t.timers.myFunction;
setTimeout(() => {
    console.log(results.parse(
        results.duration()
    )); // total duration of all ticks
    console.log(results.parse(
        results.min()
    ));      // minimal tick duration
    console.log(results.parse(
        results.max()
    ));      // maximal tick duration
    console.log(results.parse(
        results.mean()
    ));     // mean tick duration
    console.log(results.parse(
        results.median()
    ));   // median tick duration
}, 101);

```

#### Instantiating the ticks yourself

```javascript
const t = require('exectimer');
const Tick = t.Tick;

for(var i = 0; i < 10; i++) {
    // unique contexts to avoid aliasing (#9)
    (function () {
        var tick = new Tick("myFunction");
        tick.start();

        setTimeout(function() {
            tick.stop();
        }, Math.random() * 10);
    })();
}

// Display the results
var results = t.timers.myFunction;
setTimeout(() => {
    console.log(results.parse(
        results.duration()
    )); // total duration of all ticks
    console.log(results.parse(
        results.min()
    ));      // minimal tick duration
    console.log(results.parse(
        results.max()
    ));      // maximal tick duration
    console.log(results.parse(
        results.mean()
    ));     // mean tick duration
    console.log(results.parse(
        results.median()
    ));   // median tick duration
}, 101);

```

API
---

### Tick
 A tick is used to measure the difference between two execution points. All ticks are than used to calculate the average, median, min, max etc.
 Takes the name of the timer as an argument.

#### Tick.wrap()

 **Arguments**

 1. [`name`] *(String)* Name of the function
 1. `callback` *(Function)* Function for which to calculate the duration
 
 - **callback** Can be a function or a generator
     1. [`done`] *(Function)* Should be passed if function is asynchronous

 Static function that takes a name and a function as arguments. If the name is omitted than it tries to read the name of the function or it just uses "anon".
 If `done` function is not requested than it presumes that the call is synchronous.
 It also accepts a generator function in which case `done` function is not necessary.
 
#### Tick.prototype.start()
 Starts the timer of this tick.
 
#### Tick.prototype.stop()
 Stops the timer of this tick.
 
###Timers
 Array of timers. Each timer has methods to calculate the various metrics. When a tick is created, it is pushed into the
 timer with name that was passed to the ticker in the constructor.
 
```
var tick = new t.Tick("TIMER");

tick.start();
// Do some processing
tick.stop();

var myTimer = t.timers.TIMER;

console.log("It took: " + myTimer.duration());
```
 You can name your timer however you want.
 
#### Timers.TIMER.min()
 Get the shortest tick.

#### Timers.TIMER.max()
 Get the longest tick.

#### Timers.TIMER.mean()
 Get the average tick.

#### Timers.TIMER.median()
 Get the median tick.

#### Timers.TIMER.duration()
 Get the total duration of all ticks.

#### Timers.TIMER.count()
 Get the number of ticks.

#### Timers.TIMER.parse()
 Parse the output of the previous methods from nanoseconds to us, ms, ns or seconds.

---
_Source: https://npm.io/package/exectimer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
