Options
All
  • Public
  • Public/Protected
  • All
Menu

Class StopWatch

Counts the duration of runtime operations, with pausing capabilities and time segmentation — which are arbitry divisions of time.

An instance of StopWatch is able to count the duration between its start and stop, pausing, which temporarily stops the time counting, and create laps, which splits the stopwatch . Every start-stop cycle resets the object to its default state.

See BasicTimer and Timer for simpler timer implentations (less overhead).


Examples

  1. Using a single StopWatch to perform consecutive countings

    import { StopWatch } from "timecount/utils";
    
    const stopwatch = new StopWatch();
    
    while (thereAreThingsToDo) {
        stopwatch.start();
    
        doTheThings();
    
        stopwatch.stop().to("second");
        // 1.2021 s
    }
    
  2. Using a TimeWriter to write StopWatch results

    import { TimeWriter } from "timecount";
    
    const stopwatch = new StopWatch();
    const timeWriter = new TimeWriter({ verbose: true });
    
    stopwatch.start();
    
    doSomething();
    
    const time = timer.stop();
    
    timeWriter.write(time, "millisecond");
    // 10156.663207 milliseconds
    
    timeWriter.write(time, "second");
    // 10.156663207 seconds
    
    timeWriter.write(time, "minute");
    // 0.169277720116666668 minute
    
  3. Pausing and resuming the stopwatch

    const stopwatch = new StopWatch(true);
    
    thingsYouWantMeasured(); // Let's say this takes 5 seconds
    
    stopwatch.pause();
    
    thingsYouDontWantMeasured(); // And this another 5 seconds
    
    stopwatch.stop().to("second");
    // 5 s (paused time is not counted)
    
  4. Creating time categories according to the stopwatch laps

    import { TimeWriter } from "timecount";
    import { StopWatch } from "timecount/utils";
    
    const stopWatch = new StopWatch(true);
    const timeWriter = new TimeWriter({ verbose: true });
    
    doCategory1();
    
    "Category 1: " + timeWriter.write(stopWatch.endLap(), "second");
    // Category 1: 1.454 seconds
    
    doCategory2();
    
    console.log("Category 2 - ", timeWriter.write(stopWatch.endLap(), "second"));
    // Category 2: 3.092017 seconds
    
    stopWatch.pause()
    
    unimportantThing();
    
    console.log("TOTAL - ", timeWriter.write(stopWatch.stop(), "second"));
    // Result: Category 1 - 4.589632 seconds
    

Hierarchy

Index

Constructors

constructor

  • new StopWatch(autoStart?: boolean): StopWatch
  • Initializes a new instance of the BasicTimer class, optionally auto-starting it.

    Parameters

    • Default value autoStart: boolean = false

      Determines whether the timer will immediately start; default is false.

    Returns StopWatch

Properties

Protected _lapPartialTimes

_lapPartialTimes: Decimal[] = []

Protected field with the array of partial lap time values.

Protected Optional _lapStartTime

_lapStartTime: Decimal

Protected field with the time value representing the moment the lap started.

Protected Optional _pauseStartTime

_pauseStartTime: Decimal

Protected field with the time value representing the moment the pausing started.

Protected _pauseTimeSum

_pauseTimeSum: Decimal = new Decimal(0)

Protected field with the time value representing the sum of all previous pause times.

Protected Optional _startTime

_startTime: Decimal

Protected field with the time value representing the moment the timer started.

Accessors

currentLapElapsedTime

  • get currentLapElapsedTime(): Time
  • Gets the time elapsed since the start of the current lap until now.

    Returns Time

currentLapStartTime

  • get currentLapStartTime(): Time
  • Gets the time representing the moment the current lap started.

    deprecated

    Since v1.1.0 - Unnecessary property (will be removed in v2).

    Returns Time

elapsedPauseTime

  • get elapsedPauseTime(): Time

elapsedTime

  • get elapsedTime(): Time

isPaused

  • get isPaused(): boolean

isRunning

  • get isRunning(): boolean

lapCount

  • get lapCount(): number
  • Gets the number of laps currently stored at the stopwatch.

    Returns number

lapPartials

  • get lapPartials(): Time[]
  • Gets an array with time partials of the laps of the stopwatch, excluding the current.

    deprecated

    Since v1.1.0 - In favor of partialTimes (will be removed in v2).

    Returns Time[]

partialTimes

  • get partialTimes(): Time[]
  • Gets an array with time partials of the laps of the stopwatch, including the current. The sum of these times is equivalent to the total elapsed time of the stopwatch.

    Returns Time[]

Protected timerErrorType

  • get timerErrorType(): string

totalPauseTime

  • get totalPauseTime(): Time

Methods

createTimeSegment

  • createTimeSegment(): Time

endLap

  • Finishes the current lap and starts a new one. This will add a new entry to partial times and start counting time for the new lap.

    Note: The first lap always start in parallel with the stopwatch.

    throws

    TimerError When the stopwatch has not yet started.

    Returns Time

    The partial time of the finished lap (elapsed time from the start of the lap until now).

Protected getElapsedTime

  • getElapsedTime(): Time

Protected getIsRunning

  • getIsRunning(): boolean

pause

  • pause(): Decimal

resume

  • Resumes the time counting, recovering the object from a paused state.

    throws

    TimerError When the timer has not yet started -or- it is not paused.

    Returns Time

    The amount of time the timer spent paused.

start

  • start(): Decimal
  • Begins to count time, setting the start time to a value equivalent to now. This also starts the first lap of the stopwatch.

    throws

    TimerError When the stopwatch is already running.

    Returns Decimal

    Time that represents now (or better yet, the moment the stopwatch has started).

stop

  • Ends the time counting, returning the total elapsed time (sum of all lap partials) and resetting the object to its default state. This will automatically end the last created lap!

    If the stopwatch is paused, there is no need to resume before calling this method.

    throws

    TimerError When the stopwatch has not yet started.

    Parameters

    • Optional detail: StopWatchDetail

      A closure that can be used to retrieve additional data about the stopwatch. This information would otherwise be lost after the method is completed.

    Returns Time

    A time with the total amount of nanoseconds spent between start and now.

Generated using TypeDoc