boss-timer
Version:
Timer for node
122 lines (115 loc) • 3.61 kB
text/typescript
function calculateDifference (t1:Array<number>, t2:Array<number>): Array<number> {
return [ t1[0] - t2[0], t1[1] - t2[1] ]
}
function addTimes (t1:Array<number>, t2:Array<number>): Array<number> {
return [ t1[0] + t2[0], t1[1] + t2[1] ]
}
export class Timer {
private version: string = "0.1.0";
//
public start_time: Array<number> = null
public intervals: Array<number> = []
public total : Array<number> = [0,0]
public end_time: Array<number> = null
public split_times: Array<Array<number>> = []
public debug:boolean = false
public running:boolean = false
/**
* Constructor for the class
* @param {boolean} start? - Optional: starts the time on creation of the class
* @returns {Timer} this returns itself to allow command chaining
*/
constructor(start: boolean) {
if (start !== false) {
this.start_time = process.hrtime()
this.running = true
}
}
/**
* Starts the timer
* @returns {Timer} this returns itself to allow command chaining
*/
start(): Timer {
if (!this.running) {
this.start_time = process.hrtime()
this.running = true
} else {
if(this.debug) console.log('[boss-timer] Timer already running')
}
return this
}
/**
* Stops the timer
* @returns {Timer} this returns itself to allow command chaining
*/
stop(): Timer {
if(!this.running){
if(this.debug){
console.log('[boss-timer] Timer is not running')
}
} else {
this.end_time = process.hrtime()
}
this.total = calculateDifference(this.end_time, this.start_time)
this.end_time = null
this.start_time = null
this.running = false
return this
}
/**
* Creates a split time in the timer
* @returns {Timer} this returns itself to allow command chaining
*/
split(): Timer {
this.split_times.push(process.hrtime())
return this
}
/**
* Returns the elapsed time between when the timer was started and the end time, or the time elapsed so far
* @param {number} split? array index of the the split time from which to calculate the elapsed time from the start time
* @returns {number} time in miliseconds that have elapsed
*/
elapsed(split?: number): Array<number> {
var time: Array<number> = []
if (split && this.split_times.length > 0) {
if (split === -1) { // get last split
time = calculateDifference(this.split_times[this.split_times.length - 1], this.start_time)
} else {
if (typeof this.split_times[split] !== 'undefined') {
time = calculateDifference( this.split_times[split], this.start_time)
} else {
if(this.debug){
console.log('[boss-timer] Split time of index:"' + split + '" not found!')
}
}
}
} else if(split && this.split_times.length){
if(this.debug){
console.log('[boss-timer] No split times')
}
} else if (!this.running) {
time = this.total
} else {
time = addTimes(this.total, process.hrtime(this.start_time))
}
return time
}
/**
* Renders the time in a human redable format
* @param {Array<number>} time? time to render
* @returns {string} returns the time as a string with the correct time denominator
*/
render(time?:Array<number>): string {
var timeToRender = time || this.elapsed()
var ns = timeToRender[0] * 1e9 + timeToRender[1]
var ms = ns / 1e6
var s = ns / 1e9
if (ns < 1000) {
return ns + 'ns'
}
if (ms < 1000) {
return ms.toFixed(2) + 'ms'
}
return s.toFixed(2) + 's'
}
}