🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@twuni/measured

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@twuni/measured

Transparently measure the execution time of an asynchronous function.

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

Measured.js

CircleCI npm version npm downloads dependencies devDependencies license

A thin wrapper around the Node.js Performance API to transparently measure the execution time of asynchronous functions.

Installing

Using yarn:

yarn add @twuni/measured

Using npm:

npm install @twuni/measured

Usage

First, import the module:

// 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:

await doExpensiveThing();

To measure that, just change it to read something like this:

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

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`);
  }
})());

FAQs

Package last updated on 03 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts