# measure-speed

> Measure speed of sync/async functions

Latest version **1.0.0** (published 2017-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install measure-speed
pnpm add measure-speed
yarn add measure-speed
bun add measure-speed
```

## 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 | 1.0.0 |
| Published | 2017-11-02 |
| First published | 2016-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Maurizio Lupo |
| Maintainers | sithmel |
| Keywords | iterators, itertools, generators |

## Links

- npm: https://www.npmjs.com/package/measure-speed
- Repository: https://github.com/sithmel/measure-speed
- Homepage: https://github.com/sithmel/measure-speed#readme
- Issues: https://github.com/sithmel/measure-speed/issues
- npm.io page: https://npm.io/package/measure-speed

## Dependencies (3)

- [setimmediate](https://npm.io/package/setimmediate.md) ^1.0.5
- [performance-now](https://npm.io/package/performance-now.md) ^2.1.0
- [little-ds-toolkit](https://npm.io/package/little-ds-toolkit.md) ^0.4.0

## Recent versions

- 1.0.0 (latest) — 2017-11-02
- 0.1.0 — 2017-10-25
- 0.0.4 — 2017-10-25
- 0.0.3 — 2016-10-11
- 0.0.2 — 2016-10-11

## README

measure-speed
=============
This is a module for measuring the average or median speed of functions.

measureSpeed
============
This function takes as arguments a function, options object and a callback.
The callback is called with the error (null if everything is ok) and a number of milliseconds.
```js
var measureSpeed = require('measure-speed');

measureSpeed(function () {
  var a = [2, 5, 6, 3, 7, 9, 2, 3, 5, 6, 34, 234, 5, 23, 523, 4, 5, 23, 4, 5, 23, 4, 5, 2, 34];
  a.sort();
},
{ samples: 1000 },
function (err, ms) {
  // ms is the number of milliseconds. The resolution is in microseconds
});
```
The function runs a certain number of times (default 100), every run is timed, the timings are sorted and the biggest and smallest values are removed.
The result is the average of the remainings.
The options are:
* samples: the number of executions (default 100)
* discard: how many maximum and minimum timings we want to discard (default 1)
* setup: this function runs before each test
* tearDown: this function runs after each test

Measure speed works also if the function returns a promise.
```js
measureSpeed(function () {
  return Promise(function (resolve, reject) {
    setTimeout(resolve, 100);
  });
},
{ samples: 1000 },
function (err, ms) {
  // ms is the number of milliseconds. The resolution is in microseconds
});
```
Or a callback
```js
measureSpeed(function (callback) {
  myfunc(callback);
},
{ samples: 1000 },
function (err, ms) {
  // ms is the number of milliseconds. The resolution is in microseconds
});
```

measureSpeedCompare
===================
This allows to run more tests and output the difference in timings:
```js
var measureSpeedCompare = require('measure-speed/compare');

measureSpeedCompare([func1, func2, func3],
{ samples: 1000 }, // same options as measureSpeed
function (err, results) {
  // results.timings is a list of timings
  // results.timingsRel every timing divided by the first one
  // results.timingsPerc every timing divided by the first one (and then multiplied by 100)
});
```

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