Skip to content

Instantly share code, notes, and snippets.

@tjFogarty
Last active August 29, 2015 14:02
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 tjFogarty/ce385647f71a8f17b8a8 to your computer and use it in GitHub Desktop.
Save tjFogarty/ce385647f71a8f17b8a8 to your computer and use it in GitHub Desktop.
This doesn't include checks to see if this is supported on certain browsers.
/**
* Detects using window.performance
* We can serve small images first, then upgrade on faster connections
* See: http://mattandrews.info/talks/port80-2013/#/
*/
(function () {
var perf = window.performance,
start = perf.timing.requestStart, // just before page is requested
end = perf.timing.responseStart, // first byte received
totalTime = end - start;
/** Responsible for upgrading images */
var Performance = {
images: document.getElementsByTagName('img'),
timing: totalTime,
/** Kick things off */
init: function () {
this.imageSize();
},
/** Loop through images, detecting if they have a 'data-large' attribute */
imageSize: function () {
var largeSrc = null,
image = null;
for (var i = Performance.images.length - 1; i >= 0; i--) {
image = this.images[i];
largeSrc = image.getAttribute('data-large');
if (largeSrc !== null) {
image.src = largeSrc;
}
}
}
};
if (Performance.timing < 1000) {
Performance.init();
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment