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

oro-timer

v2.2.3

Published

OroTimer is a class designed for monitoring code performance.

Downloads

104

Readme

OroTimer / OTimer

Overview

OroTimer is a class designed for monitoring code performance.

It allows you to measure code execution times, which can be segmented into individual steps, each displaying its own timing and progress information.

Installation

npm install oro-timer

Example:

// cjs
const { OTimer } = require( 'oro-timer' );

// mjs, ts
import { OTimer } from 'oro-timer';

import type { OTimerTick, OTimerStep, OTimerGetTimesArgs } from 'oro-timer';
const oTimer = new OTimer('label-1');
// ... long task
oTimer.step('label-2');
// ... short task
oTimer.step('label-3');
// ... medium task
const times = oTimer.getTimes();

console.log(times);
// [
//   { label: 'label-1', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'label-2', seconds: '0.76', time: 0.761326999999582, progress: 4.715176299998536 },
//   { label: 'label-3', seconds: '1.60', time: 1.604897999999763, progress: 6.320074299998299 },
//   { label: 'total'  , seconds: '6.32', time: 6.320074299998299, progress: 6.320074299998299  }
// ]

console.log(oTimer.total);
// { label: 'total', seconds: '6.32', time: 6.320074299998299, progress: 6.320074299998299 }

Methods

new OTimer()

const oTimer = new OTimer( label?: string );

When OTimer is initialized new OTimer(), time started to run automatically.

Note: Param label it's only informative, and it helps to recognize the part of the code that is running.

let oTimer = new OTimer('do stuff');
// ... do some stuff
const times = oTimer.getTimes();

console.log(times);
// [
//   { label: 'do stuff', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'total', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 }
// ]

console.log(oTimer.total);
// { label: 'total', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 }

oTimer.start()

oTimer.start( label?: string ): void;

Because OTimer starts running when it's created, timer can be restarted with the method .start().

Note: Param label it's only informative, and it helps to recognize the part of the code that is running.

let oTimer = new OTimer('do stuff');
// ...
const times1 = oTimer.getTimes();

oTimer.start('do another stuff');
// ...
const times2 = oTimer.getTimes();

console.log(times1);
// [
//   { label: 'do stuff', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'total', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 }
// ]

console.log(times2);
// [
//   { label: 'do another stuff', seconds: '1.76', time: 1.7613269999995829, progress: 1.7613269999995829 },
//   { label: 'total', seconds: '1.76', time: 1.7613269999995829, progress: 1.7613269999995829 }
// ]

console.log(oTimer.total);
// { label: 'total', seconds: '1.76', time: 1.7613269999995829, progress: 1.7613269999995829 }

oTimer.step()

oTimer.step( label?: string ): void;

You can divide the code-times in separated steps by the method .step().

Note: Param label it's only informative, and it helps to recognize the part of the code that is running.

const { OTimer } = require('oro-timer');

let oTimer = new OTimer('first action');
// ...
oTimer.step('second action');
// ...
const times = oTimer.getTimes();

console.log(times);
// [
//   { label: 'first action', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'second action', seconds: '1.76', time: 1.7613269999995829, progress: 5.715176299998537 },
//   { label: 'total', seconds: '5.71', time: 5.715176299998537, progress: 5.715176299998537 }
// ]

console.log(oTimer.total);
// { label: 'total', seconds: '5.71', time: 5.715176299998537, progress: 5.715176299998537 }

oTimer.getTimes()

oTimer.getTimes( args?: OTimerGetTimesArgs ): OTimerStep[];

interface OTimerGetTimesArgs {
   label?: string;
   doStep?: boolean;
   addTotal?: boolean;
}

interface OTimerStep {
   label: string;    // custom label
   seconds: string;     // time in '0.00' string format
   time: number;     // step seconds
   progress: number; // timer seconds
}

Method .getTimes() is used to finish the timer and get step-times. So, by default, it does the last step automatically.

  • label ( default: 'end' ):
  • doStep ( default: true ):
    • If you want to get the same times again, you must false the first param.
  • addTotal ( default: true ):
    • By default, it adds as last item the total of oTimer. If you want to avoid this behaviour, just false the second param.
let oTimer = new OTimer('first action');
// ...
oTimer.step('second action');
// ...
const times = oTimer.getTimes({ addTotal: false });
const timesAgain = oTimer.getTimes({ doStep: false });

console.log(times);
// [
//   { label: 'first action', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'second action', seconds: '1.76', time: 1.7613269999995829, progress: 5.715176299998537 },
// ]

console.log(oTimer.total);
// { label: 'total', seconds: '5.71', time: 5.715176299998537, progress: 5.715176299998537 }

console.log(timesAgain);
// [
//   { label: 'first action', seconds: '3.95', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'second action', seconds: '1.76', time: 1.7613269999995829, progress: 5.715176299998537 },
//   { label: 'total', seconds: '5.71', time: 5.715176299998537, progress: 5.715176299998537 }
// ]

oTimer.getPerformance()

oTimer.getPerformance(): OTimerTick[];

interface OTimerTick {
   label: string; // custom label
   tick: number;  // timestamp miliseconds
}

How OTimer works, each step is saved as tick with performance.now().

With .getPerformance() you can get the millisecond timestamp of each performance-step, called tick.

let oTimer = new OTimer('first action');
// ...
oTimer.step('second action');
// ...
oTimer.getTimes();

const times = oTimer.getPerformance();

console.log(times);
// [
//   { label: 'first action', tick: 344.7998000010848 },
//   { label: 'second action', tick: 4298.6491000000388 },
//   { label: 'end', tick: 6059.9760999996217 }
// ]