Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/twuni/measured.js
Simple performance instrumentation for asynchronous functions.
https://github.com/twuni/measured.js
instrumentation javascript performance-monitoring
Last synced: 4 days ago
JSON representation
Simple performance instrumentation for asynchronous functions.
- Host: GitHub
- URL: https://github.com/twuni/measured.js
- Owner: twuni
- License: mit
- Created: 2019-09-16T17:08:42.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-04T12:08:20.000Z (about 2 years ago)
- Last Synced: 2024-12-18T16:48:32.452Z (about 1 month ago)
- Topics: instrumentation, javascript, performance-monitoring
- Language: JavaScript
- Size: 500 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Measured.js
[![CircleCI][1]][2]
[![npm version][3]][4]
[![npm downloads][5]][4]
[![dependencies][6]][7]
[![devDependencies][8]][7]
[![license][9]][10]A thin wrapper around the Node.js Performance API to transparently
measure the execution time of asynchronous functions.## Installing
Using [`yarn`][11]:
```
yarn add @twuni/measured
```Using [`npm`][12]:
```
npm install @twuni/measured
```## Usage
First, import the module:
```javascript
// Using ES6 modules:
import { measured } from '@twuni/measured';// ...or, if you are using CommonJS modules:
const { measured } = require('@twuni/measured');
```Then, you can use the `measured()` function like this:
If this is what your code looked like **before**:
```javascript
await doExpensiveThing();
```To measure that, just change it to read something like this:
```javascript
await measured(doExpensiveThing, {
onComplete: ({ duration }) => {
console.debug(`Completed in ${duration}ms`);
},onReject: ({ duration }) => {
console.debug(`Rejected in ${duration}ms`);
},onResolve: ({ duration }) => {
console.debug(`Resolved in ${duration}ms`);
}
})();
```Each of these callbacks is optional and is described as follows:
* The `#onComplete()` function will run whether the wrapped behavior resolves or rejects.
* The `#onResolve()` function will run only when the wrapped behavior resolves.
* The `#onReject()` function will run only when the wrapped behavior rejects.
## Examples
### Express.js Middleware
```javascript
const express = require('express');
const app = express();app.use((request, response, next) => measured(next, {
onComplete: ({ duration }) => {
console.log(`${request.method} ${request.path} served in ${duration}ms`);
}
})());
```[1]: https://img.shields.io/circleci/build/github/twuni/measured.js
[2]: https://circleci.com/gh/twuni/measured.js
[3]: https://img.shields.io/npm/v/@twuni/measured.svg
[4]: https://www.npmjs.com/package/@twuni/measured
[5]: https://img.shields.io/npm/dt/@twuni/measured.svg
[6]: https://img.shields.io/david/twuni/measured.js.svg
[7]: https://github.com/twuni/measured.js/blob/master/package.json
[8]: https://img.shields.io/david/dev/twuni/measured.js.svg
[9]: https://img.shields.io/github/license/twuni/measured.js.svg
[10]: https://github.com/twuni/measured.js/blob/master/LICENSE.md
[11]: https://yarnpkg.com/
[12]: https://npmjs.com/package/npm