# 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.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
autoStart |
boolean
|
<optional> |
false | When |
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.
# laps
This method returns the internal lap array as a read-only view to prevent external mutation.
Retrieves the recorded lap history.
# 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.
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.
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. |
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.
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.
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.
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.
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.
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.
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.
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.
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.
The elapsed time, formatted for presentation.
string