mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00

* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
93 lines
2.2 KiB
JavaScript
93 lines
2.2 KiB
JavaScript
/**
|
|
* @author Nandan V
|
|
* Sunday, 26 July 2020 8:33 AM
|
|
* @description Singleton class that handles the <b>timing of tests</b> and
|
|
* specs. <br/> The class is singleton as <u>javascript does not support
|
|
* multiple timer instances<u/>.
|
|
*/
|
|
class IntervalTimer {
|
|
/**
|
|
* @description Constructor for Timer.
|
|
* @param interval Sets the interval for running the timer.
|
|
* @param callBack The callback function to be executed.
|
|
* @return {IntervalTimer} If exists, the existing object.
|
|
*/
|
|
constructor(interval = 10, callBack = () => {}) {
|
|
this.prevInterval = 0
|
|
if (this.instance == null) {
|
|
this.interval = interval
|
|
this.callBack = callBack
|
|
this.instance = this
|
|
} else {
|
|
return this.instance
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Starts the timer.
|
|
*/
|
|
startTimer() {
|
|
this.timer = setInterval(this.callBack, this.interval)
|
|
}
|
|
|
|
/**
|
|
* @description Resets the timer.
|
|
* @return {number} Elapsed time in milliseconds.
|
|
*/
|
|
resetTimer() {
|
|
clearInterval(this.timer)
|
|
this.callBack = () => {}
|
|
return this.getElapsedTime()
|
|
}
|
|
|
|
/**
|
|
* @return {number} Elapsed time in milliseconds since reset.
|
|
*/
|
|
getElapsedTime(offset = 0) {
|
|
this.timeElapsed = this.timer - this.prevInterval
|
|
this.prevInterval = this.timer
|
|
return this.timeElapsed - offset
|
|
}
|
|
|
|
/**
|
|
* @return {number} Elapsed time since start.
|
|
*/
|
|
getRunTime() {
|
|
return this.timer
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @author Nandan V
|
|
* Saturday, 01 August 2020 8:33 AM
|
|
* @description Example usage
|
|
*/
|
|
const ExampleIntervalTimer = function (output = (v) => console.log(v)) {
|
|
/**
|
|
* Create am object with default settings.
|
|
* @type {IntervalTimer} Used to get timing information.
|
|
*/
|
|
const timer = new IntervalTimer()
|
|
timer.startTimer()
|
|
|
|
// ... Initialization code ...
|
|
// I generally use it for timing tests in Jasmine JS.
|
|
|
|
/**
|
|
* Gets the runtime till this point.
|
|
* Can be subtracted from ElapsedTime to offset timing of initialization code.
|
|
*/
|
|
const initOffset = timer.getRunTime()
|
|
|
|
// ... A test ...
|
|
// The time taken to run the test.
|
|
output(timer.getElapsedTime(initOffset))
|
|
|
|
/**
|
|
* Returns the elapsed time and resets the timer to 0.
|
|
*/
|
|
output(timer.resetTimer())
|
|
}
|
|
|
|
export { IntervalTimer, ExampleIntervalTimer }
|