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

2.0.6 • Public • Published

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', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'label-2', time: 0.761326999999582, progress: 4.715176299998536 },
//   { label: 'label-3', time: 1.614897999999763, progress: 6.330074299998299 },
//   { label: 'total'  , time: 6.330074299998299, progress: 6.330074299998299  }
// ]

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', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'total', 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', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'total', time: 3.953849299998954, progress: 3.953849299998954 }
// ]
console.log( times2 );
// [
//   { label: 'do another stuff', time: 1.7613269999995829, progress: 1.7613269999995829 },
//   { label: 'total', 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', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'second action', time: 1.7613269999995829, progress: 5.715176299998537 },
//   { label: 'total', time: 5.715176299998537, progress: 5.715176299998537 }
// ]

oTimer.getTimes()

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

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

interface OTimerStep {
   label: string;    // custom label
   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.

  1. doStep ( default: true ):

    If you want to get the same times again, you must false the first param.

  2. 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', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'second action', time: 1.7613269999995829, progress: 5.715176299998537 },
// ]
console.log( timesAgain );
// [
//   { label: 'first action', time: 3.953849299998954, progress: 3.953849299998954 },
//   { label: 'second action', time: 1.7613269999995829, progress: 5.715176299998537 },
//   { label: 'total', 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.step();

const times = oTimer.getPerformance();

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

Readme

Keywords

Package Sidebar

Install

npm i oro-timer

Weekly Downloads

1

Version

2.0.6

License

MIT

Unpacked Size

20 kB

Total Files

9

Last publish

Collaborators

  • oropesa