Class

StopWatch

StopWatch(autoStartopt)

Constructor

# new StopWatch(autoStartopt)

This class tracks elapsed time using the highest precision timer available. It supports pausing, resuming, and recording labeled laps for diagnostics and benchmarking.

A high-resolution stopwatch with pause, resume, and lap tracking.

sequenceDiagram participant Client participant StopWatch participant Clock as now() Client->>StopWatch: start() StopWatch->>Clock: now() Clock-->>StopWatch: timestamp Client->>StopWatch: lap() StopWatch->>Clock: now() Clock-->>StopWatch: timestamp StopWatch-->>Client: Lap Client->>StopWatch: pause() StopWatch->>Clock: now() Clock-->>StopWatch: timestamp
Parameters:
Name Type Attributes Default Description
autoStart boolean <optional>
false

When true, the stopwatch starts immediately upon construction.

View Source time.ts, line 27

Example
const sw = new StopWatch(true);
// ... work ...
const lap = sw.lap("phase 1");
sw.pause();
console.log(`Elapsed: ${lap.totalMs}ms`);

Members

# elapsedMs

This method computes the total elapsed time in milliseconds, including the current session if it is running.

The elapsed time that has been captured by the stopwatch.

View Source time.ts, line 77

# laps

This method returns the internal lap array as a read-only view to prevent external mutation.

Retrieves the recorded lap history.

View Source time.ts, line 165

# running

This method returns true when timing is in progress, and false when it is paused or stopped.

Indicates whether the stopwatch is actively running.

View Source time.ts, line 69

Methods

# elapsedMs() → {number}

This method computes the total elapsed time in milliseconds, including the current session if it is running.

The elapsed time that has been captured by the stopwatch.

View Source time.ts, line 248

The milliseconds that have elapsed since the stopwatch started.

number

# lap(labelopt) → {Lap}

This method stores the lap metadata, updates the cumulative tracking, and returns the newly created Lap.

Records a lap split since the stopwatch started, or since the previous lap.

Parameters:
Name Type Attributes Description
label string <optional>

An optional label that describes the lap.

View Source time.ts, line 291

A lap snapshot that captures incremental and cumulative timings.

Lap

# laps() → {Array.<Lap>}

This method returns the internal lap array as a read-only view to prevent external mutation.

Retrieves the recorded lap history.

View Source time.ts, line 298

The laps that have been captured by the stopwatch.

Array.<Lap>

# pause() → {this}

This method captures the partial duration, updates the accumulator, and keeps the stopwatch ready to resume later.

Pauses timing and accumulates the elapsed milliseconds.

View Source time.ts, line 262

A fluent reference to the stopwatch.

this

# reset() → {this}

This method clears the elapsed time and lap history, and preserves whether the stopwatch should continue ticking.

Resets the stopwatch state, while optionally continuing to run.

View Source time.ts, line 283

A fluent reference to the stopwatch.

this

# resume() → {this}

This method captures a fresh start timestamp, while keeping the previous elapsed time intact.

Resumes timing after a pause.

View Source time.ts, line 269

A fluent reference to the stopwatch.

this

# running() → {boolean}

This method returns true when timing is in progress, and false when it is paused or stopped.

Indicates whether the stopwatch is actively running.

View Source time.ts, line 241

The current running state.

boolean

# start() → {this}

This method records the current timestamp and transitions the stopwatch into the running state.

Starts timing if the stopwatch is not already running.

View Source time.ts, line 255

A fluent reference to the stopwatch.

this

# stop() → {number}

This method invokes StopWatch.pause to consolidate the elapsed time, and leaves the stopwatch in a non-running state.

Stops timing and returns the total elapsed milliseconds.

View Source time.ts, line 276

The milliseconds that have accumulated across all runs.

number

# toJSON() → {Object}

This method provides a JSON-friendly snapshot that includes the running state, elapsed time, and lap details.

Serializes the stopwatch state.

View Source time.ts, line 312

A serializable stopwatch representation.

Object

# toString() → {string}

This method uses formatMs to produce an hh:mm:ss.mmm string for display and logging.

Formats the elapsed time in a human-readable representation.

View Source time.ts, line 305

The elapsed time, formatted for presentation.

string