Overview
SpeedTest is the main class exported by @cloudflare/speedtest. Instantiate it with new to create and optionally auto-start a measurement run.
Constructor
- Merges the provided
configwith default values. - Creates an internal
Resultsobject to accumulate measurements. - Calls
play()automatically ifautoStartistrue(the default).
Optional configuration object. All fields are optional — omitting
config
entirely runs the engine with default settings. See
ConfigOptions for the full field reference.Properties
All properties are read-only getters.The current
Results object. Methods on this object return partial values while the
test is still running and their final values once all measurements complete.true while the engine is actively performing measurements; false when
paused, not yet started, or finished.true once all measurements in the sequence have completed. The engine does
not transition back to false after finishing — call restart() to begin a
fresh run.Methods
play()
- Clears the browser’s
PerformanceResourceTimingbuffer and sets its size to 10,000 entries before starting. - Does nothing if the engine is already running (
isRunning === true). - Does nothing if the engine has finished (
isFinished === true). Callrestart()instead.
pause()
- Sets
isRunningtofalseand triggersonRunningChange(false). - Only has an effect on pausable measurement types:
latency,download, andupload. ApacketLossmeasurement in progress runs to completion before the pause takes effect. - Does nothing if the engine is already paused or finished.
restart()
- Destroys any in-progress measurement engine.
- Resets
isRunningandisFinishedtofalse. - Calls
play()immediately after clearing.
Event callbacks
Assign functions to these properties to receive notifications as the engine runs. You can reassign them at any time before or after starting the engine.Called whenever the engine starts or stops.
running is the new value of
isRunning. See Events reference for details.Called whenever any measurement result is updated.
type is the measurement
category that changed ("latency", "download", "upload", or
"packetLoss"). This fires many times during a test run. See Events
reference for details.Called when the engine advances to the next measurement in the sequence.
measurementId is the zero-based index into the measurements array. See
Events reference for details.Called once when all measurements are complete.
results is the final
Results object with all values available. See Events
reference for details.Called when a measurement error occurs (for example, a connection failure or
TURN server credential error). The engine skips the failing measurement and
continues to the next one. See Events reference for
details.
Complete example
The following example wires up all callbacks, controls playback, and reads results.Assigning
onFinish after the engine has already started is safe — the
callback is stored and invoked when measurements complete regardless of when
it was assigned.