Pages the user visited

Measure if a certain page resource is in a user's cache.

by Schepp

JavaScript

var bench = function (index, nocache) {
    var src = pages[index].img + (nocache ? '' : '?nocache=' + (new Date()).getTime()),
            image = new Image(),
            start = (new Date()).getTime();
        
        image.onload = function() {
            var end = (new Date()).getTime();
            pages[index].results.push(end - start);
        };
        
        image.src = src;
        document.body.appendChild(image);
    },
    pages = [
        {
            name: 'Golem',
            img: 'http://suche.golem.de/staticrl/images/logo-g.png',
            results: []
        },
        {
            name: 'Heise Online',
            img: 'http://www.heise.de/icons/ho/heise_online_logo_top.gif',
            results: []
        }
    ];



pages.forEach(function(entry, index) { 
    bench(index);
    bench(index, true);
});

window.setTimeout(function() {
    pages.forEach(function(entry) { 
        var max = Math.max(entry.results[0], entry.results[1]),
            min = Math.min(entry.results[0], entry.results[1]),
            delta = max / min;
        
        if (delta > 1.25) {
            console.log('Delta: ' + delta + ' - User has visited ' + entry.name);
        } else {
            console.log('Delta: ' + delta + ' - User has not visited ' + entry.name);
        }

        console.log(entry.results);
    });
}, 1000);