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

code-execution-timer

v2.0.0

Published

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.

Downloads

37

Readme

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

:point_right: 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.

:point_right: 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

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

resetTimer(): this

Returns: The current instance of CodeExecutionTimer for method chaining.

:point_right: 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.