code-execution-timer
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Code Execution Timer

install size Coverage Status NPM Downloads GitHub License

Javascript utility for tracking and logging execution times for code sections. It allows for multiple log entries and provides options to display or return them for further analysis.

Table of Contents

Install

You can install it by running:

npm install code-execution-timer

or using yarn:

yarn add code-execution-timer

Usage

import { CodeExecutionTimer } from 'code-execution-timer';

// Initialize the timer
const timer = new CodeExecutionTimer('My Task Timer');

// Log some execution sections with time delays
setTimeout(() => {
  timer.log('Started first section');
  
  setTimeout(() => {
    timer.log('Started second section');
    
    // Complete the timer and print logs
    const logs = timer.complete(true);
    
    // You can also get the logs without printing by omitting the 'true' parameter
    console.log('Logged Results:', logs);
  }, 500);
}, 300);

When the above code is executed, the expected console output will resemble the following (the actual times may vary):

[Execution Timer Log] My Task Timer
* Started first section ===> 301ms
* Started second section ===> 501ms

Logged Results: [
  { description: 'Started first section', duration: 301 },
  { description: 'Started second section', duration: 501 }
]

Extending CodeExecutionTimer for Custom Logging

You can customize to serve your own logging strategy. Here’s an example of a custom logging class:

import { CodeExecutionTimer } from 'code-execution-timer';

class CustomExecutionTimer extends CodeExecutionTimer {
  constructor(label) {
    super(label);
  }

  /**
   * Override the printEntries method to customize the logging output.
   * This example formats the log entries as JSON and outputs to the console.
   * @param logEntries
   */
  printEntries(logEntries) {
    console.log(`[Custom Log] ${this.label} - JSON Output:`);
    logEntries.forEach(({ description, duration }) => {
      console.log(JSON.stringify({ description, duration }, null, 2));
    });
  }
}

// Example usage of the CustomExecutionTimer
const customTimer = new CustomExecutionTimer('Custom Task');

// Log some operations
customTimer.log('Step 1');
setTimeout(() => {
  customTimer.log('Step 2');
  
  setTimeout(() => {
    customTimer.complete(true); // This will use the custom printEntries method
  }, 200);
}, 100);

API

Class: CodeExecutionTimer

Constructor

constructor(label: string)
  • label (string): A descriptive label for the timer, used for identification when printing logs.

Methods

👉 log: Logs the duration between the last recorded timestamp and the current time, creating a new log entry

log(description: string, startTimestamp?: number): this
  • description (string): A description of the code section being timed.
  • startTimestamp (number, optional): The timestamp to start the timing from. Defaults to the last recorded timestamp. Returns: The current instance of CodeExecutionTimer for method chaining.

👉 complete: Returns the list of log entries and resets the timer for reuse

complete(shouldPrint?: boolean): LogEntry[]
  • shouldPrint (boolean, optional): If true, the log entries will be printed to the console. Defaults to false. Returns: An array of LogEntry objects representing the logged entries

👉 resetTimer: Resets the log entries and internal timestamps for fresh logging

resetTimer(): this

Returns: The current instance of CodeExecutionTimer for method chaining.

👉 printEntries: Outputs the log entries to the console in a readable format

printEntries(logEntries: LogEntry[]): void
  • logEntries (LogEntry[]): An array of log entries to print.

Data Types

LogEntry

type LogEntry = {
  description: string;  // A description of the code section being timed
  duration: number;     // The elapsed time in milliseconds for this log entry
}

Maintainer

@thaibalong7

Contributing

Please contribute! Look at the issues.

Package Sidebar

Install

npm i code-execution-timer

Weekly Downloads

4

Version

2.0.0

License

MIT

Unpacked Size

14.6 kB

Total Files

7

Last publish

Collaborators

  • thaibalong7