The shortest path to a result is a single line. Instantiate SpeedTest and assign onFinish — the engine starts automatically and calls your handler when all measurements are done:
import SpeedTest from '@cloudflare/speedtest';new SpeedTest().onFinish = results => console.log(results.getSummary());
getSummary() returns an object with the key metrics:
{ download: 94500000, // bps upload: 22300000, // bps latency: 12, // ms (unloaded) jitter: 1, // ms downLoadedLatency: 18, // ms (latency during download) downLoadedJitter: 3, // ms upLoadedLatency: 24, // ms (latency during upload) upLoadedJitter: 5, // ms packetLoss: 0, // ratio 0–1}
Values are undefined for any measurement that did not run or did not produce enough data points. Bandwidth values are in bits per second (bps), not bytes.
By default the engine starts measuring immediately on instantiation. Set autoStart: false to defer measurement until you call engine.play() — useful when you want the user to trigger the test:
import SpeedTest from '@cloudflare/speedtest';const engine = new SpeedTest({ autoStart: false });engine.onFinish = results => console.log(results.getSummary());// Start when ready — for example, on a button clickdocument.getElementById('start-btn').addEventListener('click', () => { engine.play();});
You can also pause and resume in progress:
engine.pause(); // pauses the current measurementengine.play(); // resumes where it left offengine.restart(); // clears results and starts over from the beginning