Socket
Book a DemoInstallSign in
Socket

debog

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

debog

TypeScript decorator to add class performance timing

0.2.0
latest
Source
npmnpm
Version published
Weekly downloads
5
400%
Maintainers
1
Weekly downloads
 
Created
Source

Debog

npm david-dm Build Status

A simple TypeScript decorator to add performance timing to your class methods.

minified size minzipped size

Installation

$ yarn add debog

Requirements

This library uses TypeScript decorators. Ensure experimentalDecorators is enabled in your tsconfig.json file.

{
  "compilerOptions": {
    "experimentalDecorators": true
  }
}

Usage

Import debog into a TypeScript file and append your classes with the decorator. Pass the names of any methods you want to profile. debog automatically waits for async methods to resolve before timing!

import debog from 'debog'

@debog('longMethod', 'shortMethod', 'asyncMethod')
export default class MyClass {
  longMethod = () => {
    let output = 0
    for (let i = 0; i < 100000; i++) {
      output += i
    }
    return output
  }

  shortMethod = () => {
    let output = 0
    for (let i = 0; i < 10; i++) {
      output += i
    }
    return output
  }

  asyncMethod = async () => new Promise(resolve => {
    setTimeout(resolve, 1000)
  })
}

const example = new MyClass()
example.shortMethod() // logs "shortMethod took 0ms"
example.longMethod() // logs "longMethod took 3ms"
example.asyncMethod() // logs "asyncMethod took 1000ms"

You can alternatively restrict output until a certain threshold is reached. For example, to log when a method takes longer than 5ms:

import debog from 'debog'

@debog(5, 'waitMethod', 'instantMethod')
export default class MyClass {
  waitMethod = async () => new Promise(resolve => {
    setTimeout(resolve, 10)
  })

  instantMethod = () => {
    return 42
  }
}

const example = new MyClass()
await example.waitMethod() // logs "waitMethod took 10ms"
example.instantMethod() // logs nothing

Output

You are not restricted to using console.log as output. In fact, you can output any timing call to any destination you want such as statsd, prometheus, etc.

Simply override the exposed logger attribute.

import debog from 'debog'

debog.logger = (timing: number, methodName: string) => {
  // process information
  // default: console.log(`%c%d took %sms`, 'color: red', method, timing)
}

API

This library exports one default decorator function, defined as:

interface Debog {
  (...params: [number | string, ...string[]]): any
  logger(timing: number, methodName: string): void
}

License

MIT

FAQs

Package last updated on 22 Feb 2020

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.