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

@klny/performance-test

v1.0.1

Published

log and create statistics of JS functions performance

Downloads

2

Readme

performance-test

Log performance and create performance statistics of JS functions.

Usage

Import package for example as "performance".

Package contains three methods test function performance
Functions are wrapped seamlessly with no impact on rest of your code
const performance = require('@klny/performance-test');

const logFunc = performance.log(func);                // logs wrapped function duration
const statFunc = performance.stat(func);              // creates/recalculates statistics on execution
const logAndStatFunc = performance.logAndStat(func);  // both of previous combined 
Wrap function in module exports to log/get statistics on every execution
module.exports = {
  yourFunction: performance.log(yourFunction),                 // logs duration on every execution
  yourOtherFunction: performance.stat(yourOtherFunction),      // writes statistics on every execution
  yourLastFunction: performance.logAndStat(yourLastFunction),  // logs and writes statistics on every execution
}
To access statistics use following methods
performance.getStats();    // return statistics as object
performance.logStats();    // logs statistics as text table
performance.resetStats();  // resets all statistics
There are two optional arguments to control functionality
  • enabled - wrap-time switch to enable/disable testing
  • cond - execution-time condition to test only if condition is met
// enable wrapping only in development
const logFunc = performance.log(func, process.env === 'DEVELOPMENT'); 

// always wrap but log/create stats only if first function argument is > 10
const logFunc2 = performance.log(func, true, (args) => args[0] > 10);  

Examples

Log function duration
const performance = require('@klny/performance-test');
const sleep = require('./utils').sleep;

// use log wrapper to log duration of sleep function
const logSleep = performance.log(sleep);

console.log('logging sleep:');
await logSleep(20);
await logSleep(50);
await logSleep(70);
logging sleep:
 - performance test: function sleep(20) [22.533500 ms]
 - performance test: function sleep(50) [50.854300 ms]
 - performance test: function sleep(70) [69.862901 ms]
Create statistics
const performance = require('@klny/performance-test');
const statSleep = performance.stat(require('./utils').sleep);
const statNumStr = performance.stat(require('./utils').numStr);
const statFib = performance.stat(require('./utils').fib);

performance.resetStats();
console.log('getting stats:');
for (let i = 0; i < 10; i++) {
  await statSleep(i*30);
  await statNumStr(i*37915);
  await statFib(i+30);
}
performance.logStats();
getting stats:

                          --- PERFORMANCE STATS ---

        | samples |     min    |     max    |     sum     |     avg
-----------------------------------------------------------------------------
 sleep  |      10 |   0.095001 | 270.326800 | 1350.685503 | 135.068550   [ms]
 numStr |      10 |   0.075399 |  60.670800 |  295.646498 |  29.564650   [ms]
 fib    |      10 |  11.464100 | 613.152900 | 1610.805801 | 161.080580   [ms]

Configuration

Default configuration is a JS object:

const config = {
  isNodeEnv: undefined,
  units: 'ms',
  statTableName: '--- PERFORMANCE STATS ---',
  decimalPlaces: 6,
  logFunction: console.log,
  log: ' - performance test: function ${function}(${arguments}) [${duration} ${units}]'
};
  • isNodeEnv - boolean signalizing node env - uses process.hrtime() for current time if true, Date() otherwise
  • units - units for duration calculation ['s', 'ms', 'us', 'ns']
  • statTableName - name of table with statistics, provide empty string to skip
  • decimalPlaces - number of decimal places for rounding numbers
  • logFunction - your logger function to use, e.g. console.log or winston logger
  • log - log string formatter, use provided placeholders for dynamic values

Use "configure" method to change configuration.

Specify any or all configuration keys. Default values will be used for missing keys.

const performance = require('@klny/performance-test');

performance.configure({
  statTableName: 'Performance Table',
  units: 'us',
  decimalPlaces: 3
});

performance.logStats();
                              Performance Table

        | samples |    min    |     max    |     sum     |     avg
----------------------------------------------------------------------------
 sleep  |      10 |    95.001 | 270326.800 | 1350685.503 | 135068.550   [us]
 numStr |      10 |    75.399 |  60670.800 |  295646.498 |  29564.650   [us]
 fib    |      10 | 11464.100 | 613152.900 | 1610805.801 | 161080.580   [us]

Features

  • seamless function wrapping
  • no impact on existing application
  • sync/async function support
  • wrap-time and execution-time control
  • statistics visualization
  • access to statistics object
  • configurable logging function

Installation

$ npm install @klny/performance-test

License

MIT