Skip to content

A simple TypeScript decorator to add performance timing to class methods

License

Notifications You must be signed in to change notification settings

teamzerolabs/debog

 
 

Repository files navigation

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. Simply prepend a * before the method name to profile an async method.

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

API

This library exports one default decorator function, defined as:

function debog(...params: [number | string, ...string[]]);

License

MIT

About

A simple TypeScript decorator to add performance timing to class methods

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • TypeScript 92.1%
  • JavaScript 7.9%