Freitag, 23. Januar 2015

Detect client speed using JavaScript

You can detect how long it toke for the browser to load the page. With load I mean the download time. Have a look at window.performance it contains all the timings. Also see html5rocks.

if (window.performance && window.performance.timing)
{
    var download_time = (window.performance.timing.responseEnd           - window.performance.timing.responseStart);
    var speed = ($('html').html().length) / download_time;
    if (speed < 400)
    {
        alert('slow');
    }
    else    {
        alert('fast');
    }
}

Note that this works best it the page size is large. For small pages the TCP/IP overhead for is large compared to your download time. My page was about 19304 bytes and I was able to detect mobile vs desktop.

Also see this stackoverflow question.

This does not work for IE8 and Safari