npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

execution-time-tracker-decorator

v2.2.0

Published

Easily track and log execution time of your methods in Node and Browser apps

Downloads

430

Readme

execution-time-tracker-decorator

Install

In terminal, run:

npm i execution-time-tracker-decorator

Usage

Import

In your project, import the decorators you need :

import { ExecTimeAsync, ExecTimeSync } from 'execution-time-tracker-decorator';

Decorate methods

Two decorators are available:

  • @ExecTimeSync() for synchronous methods
  • @ExecTimeAsync() for asynchronous ones.

Examples:

class Demo {

	@ExecTimeSync()
	syncFunction(): number {
		let a = 0;
		for (let i = 0; i < 100; i++) {
			a++;
		}
		return a;
	}

	@ExecTimeAsync()
	async asyncFunction(): Promise<string> {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve('foo');
			}, 300);
		});
	}

}

Options

Both @ExecTimeSync() and @ExecTimeAsync() accept an optional object parameter which contain options to adapt to your needs. If one or more of these options are not provided, default values will be used.

Available options

  • title: string (default = <ClassName>::<DecoratedMethodName>), the title string to be logged.
  • shouldLogArguments: boolean (default = false), when true, arguments passed to the decorated method will be added to the logs.
  • loggerMethod: any (default = console.log), the custom logger method to use (i.e. this.logger.info if you use a custom logger).

Examples:

class Demo {

	@ExecTimeSync({ title: 'CustomTitle' })
	syncFunctionA(): number {
		let a = 0;
		for (let i = 0; i < 10000000; i++) {
			a++;
		}
		return a;
	}

	@ExecTimeSync({ shouldLogArguments: true })
	syncFunctionB(param1: number, param2: string): number {
		let a = param;
		for (let i = 0; i < 100; i++) {
			a++;
		}
		return a;
	}

	@ExecTimeSync()
	syncFunctionThrow(): number {
		let a = 0;
		for (let i = 0; i < 10000000; i++) {
			a++;
		}
		throw a;
	}

	@ExecTimeAsync({ loggerMethod: this.logger.info })
	async asyncFunction(): Promise<string> {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve('bar');
			}, 300);
		});
	}

}

Results

syncFunctionA()
CustomTitle - 8ms - Success, {
title: 'CustomTitle',
executionTime: 8,
unit: 'ms',
succeed: true,
arguments: undefined
}

syncFunctionB(5, 'stringParam')
Demo::syncFunctionB - 1ms - Success, {
title: 'Demo::syncFunctionB',
executionTime: 1,
unit: 'ms',
succeed: true,
arguments: [5, 'stringParam']
}

syncFunctionThrow()
Demo::syncFunctionThrow - 8ms - Failure, {
title: 'Demo::syncFunctionThrow',
executionTime: 8,
unit: 'ms',
succeed: false,
arguments: undefined
}

Custom logger method

When using a custom logger, be sure that the method you pass accepts multiple parameters: a main string message, and any object. You need to pass directly the method you want to be used, i.e. this.logger.info or this.logger.debug.

Logs

Using a custom logger or not, the first parameter that will be passed is the main message (<Title> - <ExecutionTimeMs> - <Success status>), the second is an object containing these properties:

{
	title: string,
	executionTime: number,
	unit: 'ms',
	succeed: boolean,
	arguments: any[] | undefined,
}

Notes

If decorators are used in a Node.js app, process.hrtime.bigint() will be used, resulting in a nanosecond precision execution time value, which will be expressed as milliseconds, i.e. 104136211ns will be logged as 104.1362ms.

Otherwise, new Date().valueOf() will be used, which has a millisecond precision.