Coding with Jesse

JavaScript Speed Detection

December 14th, 2005

This question came up on the css-discuss mailing list today (asked by Howard 'duke' Holtz):

I would like to include a Flash header on the homepage, but switch to a standard gif header for viewers with slow connections. I was hoping that there is a way to maybe have two “includes”, and use one or the other – but first I need some kind of 'conditional switch' or 'logic switch'.

This is a great question. I don't think any website has addressed this issue through scripting. We've all seen websites that start off with "Click here for Flash, Click here for slow connections" pages. But what about using JavaScript to test the speed of a connection?

I started experimenting with JavaScript and came up with a fairly simple, clean solution. It is all based on the onload event of an image. What we will do is time how long it takes to load an image. If the time is below our threshold for "high speed", we will display the flash content. Otherwise, we will do nothing and display the default content.

<script type="text/javascript">
var start = (new Date()).getTime();
var THRESHOLD = 2500; // a number we will calculate below

function speedTest() {
    var duration = (new Date()).getTime() - start;
    if (duration < THRESHOLD) {
        // fast connection (display flash)
    } else {
        // slow connection (do nothing?)
<img src="some-image.jpg" onload="speedTest()">

The onload event can go on any image on the page, preferably the largest image to give us a better estimate. Let's say we only want users downloading faster than 10k/s to get the flash code. We check the size of our some-image.jpg, and see that it is 25kb. We can easily calculate the THRESHOLD value like so: (25kb) / (10k/s) = 2.5s. Converting this to milliseconds gives us 2500, so this is what we will use for the THRESHOLD.

If you're using Bobby VanDerSluis' Unobtrusive Flash Objects 2.0, you can put the UFO code to load Flash in the block for users with a fast connection. You can then do nothing for users with a slow connection and just let them see the default content on the page.

I have thought of one problem with this solution: if the image is in the user's cache, the number will be very low. This will cause the flash to be loaded if they come back later. To avoid this, you could add a no-cache rule on the webserver for this image. Or, instead of using an image on the page, you could load an image in the background with JavaScript using a random URL like so:

var testImage = new Image();
testImage.onload = speedTest;
testImage.src = "some-image.jpg?random=" + Math.random();

Of course, if you are using PHP or another scripting language on the server, you could generate the random URL a number of different ways. This fix also has the downside that the visitor must download this extra image every time. You'll have to balance these factors in your solution to come up with one that fits.

Good luck! I'd love to hear your comments, suggestions and questions about this. Let me know how it goes on your own web sites!


1 . Mikhail Bozgounov on December 15th, 2005

Mikhail Bozgounov

...should try it one of these days.

Could this be done through PHP too?

Just an idea...

And thx for sharing this! :-)

2 . Jesse Skinner on December 15th, 2005

Jesse Skinner

Ya, it should totally be possible with PHP. I guess it would work more or less the same way.. sending a file, checking how long it takes, and perhaps setting a SESSION variable with the viewing-mode.

I'm just packing to go back to Canada for the holidays, so somebody else will have to write up the PHP solution :)

Happy Holidays,

3 . haribol on November 28th, 2007


tahank you for this script

4 . Damien on January 10th, 2008


Nice idea!

5 . Rob on February 21st, 2008


Does image size matter?

6 . nG on April 19th, 2008


I have been thinking about this as well. The only issue is using an image. The user has to load the image... that is the only major problem I see with this. Adding a timeout would help make it better, but I still cringe at the thought of making them load a file to test the speed.

7 . Rob on April 21st, 2008


I think using an image for the test is standard, but I'm no expert. I know that sites like uses images to test the speed. My boss uses a service that detects connection speeds, I wonder how they work...?

8 . Alex Le on April 22nd, 2008

Alex Le

Based on this article, I wrote a more completed solution to use javascript for speed detection on my blog



9 . MooCow on May 7th, 2008


I have a high-speed connection and I still don't want to see anything that's made in Flash. In fact I browse with disabled plug-ins because Flash is an annoyance in 99.9999999% of cases.

Don't assume that high-speed = Flash and low-speed = HTML/CSS.

10 . Rob on May 7th, 2008


MooCow...I somewhat agree with you. Being a frequent visitor of sites like, they WAY over do it with flash, but with a high-speed connection, I can't see how that can be such an annoyance. Nonetheless, you're right. I never believed in the thought of having a high-speed version of a site as well as a low-speed. That's just too much.

11 . Jesse Skinner on May 7th, 2008

Jesse Skinner

@MooCow, @Rob - I agree as well. In a perfect world, all sites would be low-speed, and people would click specifically to see large things (videos, games, etc.)

The worse thing is, most Flash sites don't even give a choice, because they don't even have a non-Flash version. Say goodbye to search engine optimization and accessibility!

12 . Steve Barett on April 21st, 2009

Steve Barett

At least the last three posts were over a year ago since they smell of typical neanderthal ignorance.

13 . Steve Barett on April 21st, 2009

Steve Barett

btw Jesse, thanks for your work on this, really useful.

14 . Rob on April 22nd, 2009


"At least the last three posts were over a year ago since they smell of typical neanderthal ignorance."

Does anyone else find it ironic how ignorant this statement is? Sounds like someone is masking their intelligence (or lack thereof) by saying "typical neanderthal ignorance". Steve, can you even explain what that means? Is "typical neanderthal ignorance" a fancy way of saying "someone who is stuck in their ways"? If so, how exactly do they fit under that description?

I (Rob) was merely saying that too much flash can impede someone as they are trying to view a webpage. Particulary on sites, like espn, where if you accidentally hover over an ad it drops down over other content and selections or if you click on a link before the page is completely loaded and the link you want to click on drops lower, so the browser registers your link click on something else.

Steve, when calling someone ignorant, generally, you should try to avoid being ignorant yourself, in the same breathe.

15 . Flithost on July 14th, 2010


What is best way to do? javascript or php ?

Comments are closed, but I'd still love to hear your thoughts.