Skip to content

Instantly share code, notes, and snippets.

@mikaelkaron
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikaelkaron/215a54f962cb57243f09 to your computer and use it in GitHub Desktop.
Save mikaelkaron/215a54f962cb57243f09 to your computer and use it in GitHub Desktop.
Simple speed tester
(function (sources, delay, success, fail) {
function onload() {
this.stop = new Date().getTime();
window.setTimeout(next, success.apply(this, arguments) || delay);
}
function onerror() {
this.stop = new Date().getTime();
window.setTimeout(next, fail.apply(this, arguments) || delay);
}
function next() {
var source = sources.pop();
var img;
if (source !== void(0)) {
img = new Image();
img.onload = onload;
img.onerror = onerror;
img.start = new Date().getTime();
img.src = source + "?" + img.start;
}
}
next();
})([
"http://example.com/img-1.jpg",
"http://example.com/img-2.jpg",
"http://example.com/img-3.jpg",
"http://example.com/img-4.jpg",
"http://example.com/img-5.jpg",
], 100, function () {
console.info("send", "timing", "SpeedTest", "sampleLoadTime", this.stop - this.start , this.src);
}, function () {
console.error("send", "timing", "SpeedTest", "sampleErrorTime", this.stop - this.start , this.src);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment