@qntm-code/progress-logger
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

@qntm-code/progress-logger

A simple progress logger for Node.js that outputs progress and estimated time remaining to the console.

GitHub release Quality Gate Status

Installation

You can install via npm or yarn.

npm

npm install --save @qntm-code/progress-logger

yarn

yarn add @qntm-code/progress-logger

Usage

Constructor Arguments

First you must create a new instance of the ProgressLogger class. The constructor takes the following arguments:

Argument Type Description
total number The total number of items to process.
message string The message to display before the progress bar.
averageMessage string The message to display before the average time per item.
averageTimeSampleSize Optional number The number of items to use when calculating the average time per item. Defaults to 100.

Methods

itemCompleted

Call itemCompleted on the ProgressLogger instance to increment the progress bar by one item. This method takes the following arguments:

Argument Type Optional Description
time number true The time taken to process the current item. This is used to calculate the average time per item.

If you don't pass a time argument when calling itemCompleted, the average time will be calculated using the durations between each time itemCompleted is called. This is useful if you are processing items batches as multiple items may be being processed at the same time.

destroy

After you've finished processing your items, you must call destroy on the ProgressLogger instance to ensure the progress logger is destroyed and prevent a memory leak.

Example

import { ProgressLogger } from '@qntm-code/progress-logger';

async function someAsyncProcess(): Promise<void> {
  // Do something
}

async function main(): Promise<void> {
  const itemsToProcess = [
    /* Some data */
  ];
  const total = itemsToProcess.length;

  const logger = new ProgressLogger({
    total,
    message: 'Processing',
    averageMessage: 'Average process time',
  });

  for (let i = 0; i < total; i++) {
    const startTime = performance.now();

    await someAsyncProcess();

    logger.itemCompleted(performance.now() - startTime);
  }

  /**
   * You must call destroy() after you've finshed processing your items to ensure the progress logger is
   * destroyed and prevent a memory leak.
   */
  logger.destroy();
}

Package Sidebar

Install

npm i @qntm-code/progress-logger

Weekly Downloads

90

Version

1.3.2

License

ISC

Unpacked Size

21.2 kB

Total Files

9

Last publish

Collaborators

  • bameyrick