Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Measure how fast an image loads: mainly to test network speed
This example is the basis of a simple WordPress plugin
which measures the download speed of a sample image
and then sends this information to the server for further action.
(e.g. writing to a database)
function measureSpeed() {
var duration = (endTime - startTime) / 1000;
var bitsLoaded = downloadSize * 8;
var speedBps = Math.round(bitsLoaded / duration);
// add your functionality to define what you want to do with the
// ajax response. e.g. set a cookie or whatever
var startTime = (new Date()).getTime(),
imageAddr = 'assets/speed.png?n=' + Math.random(), // change to match the path of the sample image
downloadSize = 1441, // file size of the sample image in bytes
download = new Image();
download.src = imageAddr;
download.onload = function () {
endTime = (new Date()).getTime();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.