oro-timer v2.2.4
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-timerExample:
// 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
timesagain, you mustfalsethe first param.
- If you want to get the same
- addTotal ( default: true ):
- By default, it adds as last item the
totalof oTimer. If you want to avoid this behaviour, justfalsethe second param.
- By default, it adds as last item the
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 }
// ]