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

parikshan

v1.0.0

Published

Compiler to measure the running time of javascript functions

Downloads

6

Readme

⏰ Parikshan

lint test build npm version

Compiler to measure the running time of javascript functions.

InstallationUsageUsing with MongoDBFAQs

Converts

// index.js
greet('John')

To

// output/index.js
const {parikshan} = require("parikshan/build/src/parikshan");

parikshan(greet)('John');

// if compiled with -s flag then compiles to
parikshan(
  greet,
  {"start":{"line":1,"column":0},"end":{"line":1,"column":13},"filename":"index.js"}
)('John')

Installation

To install this package run:

npm i parikshan -D

or with yarn run:

yarn add parikshan -D

Usage

CLI Usage

parikshan  <files..>

Compiles code to performance.measure the functions

Positional Arguments:
  files  One or more files or glob patterns to compile

Options:
  -h, --help        Show help                                          [boolean]
  -v, --version     Show version number                                [boolean]
  -o, --output-dir  Output directory                 [string] [default: "build/parikshan"]
  -s, --source-loc  Capture line, column number in performance measurement
                                                      [boolean] [default: false]

Examples:
  parikshan "{,!(node_modules)/**/}*.js"    Compile all js files except for
  -o output                                 files under node_modules to output
                                            folder
  parikshan build/src/**/*.js -so output    Compile to output folder with source
                                            location captured

API Usage

import {Compiler} from "parikshan"

const compiler = new Compiler()
const compiledFileContents = await compiler.compile(["build/src/**/*.js"], "output", true)

See docs/README.md.

Subscribing to PerformanceEntry Events

const observer = new PerformanceObserver(entryList => {
  const entries = entryList.getEntries();
  const parikshans = entries.filter(entry => entry.name === 'parikshan')
  // do anything with performance entries generated by parikshan
});

observer.observe({
  entryTypes: ['measure'],
});

Performance Entry Structure

{
  /** PerformanceEntry name */
  name: 'parikshan';
  /** A high res timeStamp representing the time value of the duration of the function */
  duration: number;
  /** A high res timeStamp representing the starting time for the performance metric. Not a UNIX timestamp */
  startTime: number;
  /** Entry type */
  entryType: 'measure';
  /** Extra details */
  detail: {
    /** Function name. 'anonymous' if function doesn't have any name */
    functionName: string;
    /** list of function's stringified arguments */
    arguments: string[];
    /** `Date` object for function called at */
    calledAt: Date;
    /** `Date` object for function returned at */
    returnedAt: Date;
    /** Location of function in source code. Will be only present when compiled with -s flag else undefined */
    location:
      | {
          start: {
            line: number;
            column: number;
          };
          end: {
            line: number;
            column: number;
          };
          filename: string;
        }
      | undefined;
  };
}

Using with MongoDB

Use package's initMongoPerfSubscriber function to store performance entries in MongoDB.

If given collection doesn't exist, it will create time series collection with given name. In case time series collection is not supported (i.e. MongoDB version below 5.0) then normal collection will be created.

Check document structure.

You can create MongoDB charts dashboard by importing Parikshan.charts file and connect it to collection. Check blog post on how to import/export dashboard.

MongoDB charts Parikshan Dashboard

Usage of initMongoPerfSubscriber

// at the start of entry/main file do
const {initMongoPerfSubscriber} = require('parikshan')
initMongoPerfSubscriber({
   dbConnectionString: process.env.DB_CONN_STRING,
   dbName: process.env.DB_NAME,
   collectionName: process.env.DB_COLLECTION,
   // optional
   deleteAfterSeconds: 60 * 60
})

FAQs

Q: Can it compile typescript ? Not directly. But you can run typescript compiler first and then compile output js files. For example tsc && parikshan build/src/**/*.js -so output

Q: Can this be used with frontend code ? Yes, before bundling you can compile it. Creating a webpack loader is in roadmap.

Q: When should this not be used ?

  1. When you are overwriting defintion for Promise.prototype.finally in your code. It may give max call stack exceeded error due to recursive calls
  2. Not recommended in production environment.

Q: Why not wrap in performance.timerify instead of parikshan ?

  1. To add additional details to performance entries.
  2. performance.timerify only works in NodeJS environment as it uses internal native binding.

Q: How was the example project shown in dashboard compiled ?

NOTE :- having Azure account is not required for this to run. So envs in .env of 43.complex-dialog can be left undefined.

// clone botbuilder samples repo
git clone https://github.com/microsoft/BotBuilder-Samples.git

// goto 43.complex-dialog example
cd BotBuilder-Samples/samples/javascript_nodejs/43.complex-dialog

// install dependencies
npm install

// install parikshan as dev dependency
npm i parikshan -D

// at the beginning of index.js, add
//
// const {initMongoPerfSubscriber} = require('parikshan')
// initMongoPerfSubscriber({
//   dbConnectionString: process.env.DB_CONN_STRING,
//   dbName: process.env.DB_NAME,
//   collectionName: process.env.DB_COLLECTION,
// })

// compile with parikshan
npx parikshan "{,!(node_modules)/**/}*.js" -s

// run compiled code
node build/parikshan/index.js

// converse with bot using botframework emulator. Check README of 43.complex-dialog for info on this.

// performance entries will be stored in collection as users are using bot.
// create your own MongoDB charts dashboard or import dashboard of this project to get insights of your code.

// Adjust refresh time in `Auto-refresh settings` of dashboard according to your need

Q: What does word "parikshan" means ? Word "parikshan" is originated from Sanskrit language meaning "examine"