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

@jointly/performance-logger

v0.2.4

Published

This is a library providing both a higher-order function and a decorator that can be used to measure the performance of a function. It can be used both with `sync` and `async` functions.

Downloads

11

Readme

What is it?

This is a library providing both a higher-order function and a decorator that can be used to measure the performance of a function.
It can be used both with sync and async functions.

How do I install it?

You can install it by using the following command:

npm install @jointly/performance-logger

How do I use it?

Both the higher-order function and the decorator can be used to measure the performance of a function.
The decorator can be used to measure the performance of a class method, while the higher-order function can be used to measure the performance of any function.

Higher-Order Function

First, you need to create a logger.
You can do this by using the logPerformance function.
Then, you can wrap the function you want to measure by using the logger.

import { logPerformance } from '@jointly/performance-logger';

const mockFn = (a, b) => a + b;

const logger = logPerformance({
  name: 'my-logger',
  validation: true,
  log: console.log
});

const loggedMockFn = logger(mockFn);

loggedMockFn(1, 2);
loggedMockFn(2, 3);

Example output (using console.log):

{
  name: 'my-logger',
  executionTime: 0.08937501907348633,
  args: [ 1, 2 ],
  result: 3
}
{
  name: 'my-logger',
  executionTime: 0.011166989803314209,
  args: [ 2, 3 ],
  result: 5
}

Decorator


import { LogPerformance } from '@jointly/performance-logger';

class MyClass {
  @LogPerformance({
    name: 'my-logger',
    validation: true,
    log: console.log
  })
  myMethod(a, b) {
    return a + b;
  }
}

const myClass = new MyClass();

myClass.myMethod(1, 2);
myClass.myMethod(2, 3);

Example output (using console.log):

{
  name: 'my-logger',
  executionTime: 0.07745802402496338,
  args: [ 1, 2 ],
  result: 3
}
{
  name: 'my-logger',
  executionTime: 0.012000024318695068,
  args: [ 2, 3 ],
  result: 5
}

Tests

You can run the tests by using the following command:

npm test

How does it work?

The decorator will measure the time it takes for a function to execute and log it to the console / the given logger.
It expects an object with the following properties:

  • name (required for higher-order function, optional for decorator): The name of the function to be measured
  • log (optional): The function to call with the measuration results. Defaults to console.log. It will be called with the following parameters:
    • name: The name of the function to be measured
    • executionTime: The time it took for the function to execute in milliseconds
    • args: The arguments passed to the function to be measured
    • result: The result of the function to be measured
  • validation (optional): A function or a boolean that will be called / verified before the function to be measured is called. Defaults to true. If it returns false, the function will be called but the measuration will not be logged.
    • If it is a function, it will be called with the arguments passed to the function to be measured.
    • If it is a boolean, it will be used as the validation result.

Other Info

The name parameter

The name parameter is used to identify the function/method to be measured.
Please, be aware that even if the LogPerformance decorator can generate a name if not passed (based on class name and property name), it could lead to unexpected labels in an environment where the code is minified, obfuscated or transpiled.
For this reason, it is highly recommended to pass a name to the decorator.