# 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.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
autoStart |
boolean
|
<optional> |
false | When true, the stopwatch starts immediately upon construction. |
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.
# laps
Returns the internal lap array as a read-only view to prevent external mutation.
Retrieves the recorded lap history.
# running
Returns true when timing is in progress and false when paused or stopped.
Indicates whether the stopwatch is actively running.
Methods
# elapsedMs() → {number}
Computes the total elapsed time in milliseconds, including the current session if running.
Elapsed time captured by the stopwatch.
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. |
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.
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.
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.
Fluent reference to the stopwatch.
this
# resume() → {this}
Captures a fresh start timestamp while keeping previous elapsed time intact.
Resumes timing after a pause.
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.
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.
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.
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.
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.
Elapsed time formatted for presentation.
string