Class

StopWatch

StopWatch(autoStartopt)

Constructor

# new StopWatch(autoStartopt)

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

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 25

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

Members

# elapsedMs

Computes the total elapsed time in milliseconds, including the current session if running.

Elapsed time captured by the stopwatch.

View Source time.ts, line 75

# laps

Returns the internal lap array as a read-only view to prevent external mutation.

Retrieves the recorded lap history.

View Source time.ts, line 163

# running

Returns true when timing is in progress and false when paused or stopped.

Indicates whether the stopwatch is actively running.

View Source time.ts, line 67

Methods

# elapsedMs() → {number}

Computes the total elapsed time in milliseconds, including the current session if running.

Elapsed time captured by the stopwatch.

View Source time.ts, line 246

Milliseconds elapsed since the stopwatch started.

number

# lap(labelopt) → {Lap}

Stores the lap metadata, updates 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>

Optional label describing the lap.

View Source time.ts, line 289

Lap snapshot capturing incremental and cumulative timings.

Lap

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

Returns the internal lap array as a read-only view to prevent external mutation.

Retrieves the recorded lap history.

View Source time.ts, line 296

Laps captured by the stopwatch.

Array.<Lap>

# pause() → {this}

Captures the partial duration, updates the accumulator, and keeps the stopwatch ready to resume later.

Pauses timing and accumulates elapsed milliseconds.

View Source time.ts, line 260

Fluent reference to the stopwatch.

this

# reset() → {this}

Clears elapsed time and lap history, preserving whether the stopwatch should continue ticking.

Resets the stopwatch state while optionally continuing to run.

View Source time.ts, line 281

Fluent reference to the stopwatch.

this

# resume() → {this}

Captures a fresh start timestamp while keeping previous elapsed time intact.

Resumes timing after a pause.

View Source time.ts, line 267

Fluent reference to the stopwatch.

this

# running() → {boolean}

Returns true when timing is in progress and false when paused or stopped.

Indicates whether the stopwatch is actively running.

View Source time.ts, line 239

Current running state.

boolean

# start() → {this}

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 253

Fluent reference to the stopwatch.

this

# stop() → {number}

Invokes StopWatch.pause to consolidate elapsed time, leaving the stopwatch in a non-running state.

Stops timing and returns the total elapsed milliseconds.

View Source time.ts, line 274

Milliseconds accumulated across all runs.

number

# toJSON() → {Object}

Provides a JSON-friendly snapshot including running state, elapsed time, and lap details.

Serializes the stopwatch state.

View Source time.ts, line 310

Serializable stopwatch representation.

Object

# toString() → {string}

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 303

Elapsed time formatted for presentation.

string